Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

call Rest web service from plugin c#

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

I have created a plugin which get activated when on create record.

And in plugin i am calling a REST api. it run very well in a simple application console

But when i am calling in plugin  like below 

public string MakeRequest(string parameters)
{
var request = (HttpWebRequest)WebRequest.Create(EndPoint + parameters);

request.Method = Method.ToString(); //get
request.ContentLength = 0;
request.ContentType = ContentType;

using (var response = (HttpWebResponse)request.GetResponse())//*******
{
var responseValue = string.Empty;

if (response.StatusCode != HttpStatusCode.OK)
{
var message = String.Format("Request failed. Received HTTP {0}", response.StatusCode);
throw new ApplicationException(message);
}

// grab the response
using (var responseStream = response.GetResponseStream())
{
if (responseStream != null)
using (var reader = new StreamReader(responseStream))
{
responseValue = reader.ReadToEnd();
}
}

return responseValue;
}
}

But its not running and when i am debugging it comes on //*******  it getting stop and my Plugin Registration Tool also get crashed.

If it don't get crash i can get the exception but due to that i am unable to catch the cause.

some one can help me 

tnx 

*This post is locked for comments

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: call Rest web service from plugin c#

    Open the plugin registration tool and make sure that the isolation mode in none not sandbox.

  • Amarsen Profile Picture
    Amarsen 55 on at
    RE: call Rest web service from plugin c#

    I think this is something you will have to workout with your network security team. Please see below

    https://community.dynamics.com/crm/f/117/t/144242 (register plugin outside sandbox)

    social.msdn.microsoft.com/.../why-request-for-the-permission-of-type-systemnetwebpermission

    blogs.iis.net/.../Enabling-WebPermission-in-Medium-Trust

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: call Rest web service from plugin c#

    good morning Amarsen

    i tried this code and i get yhis error:

    Request for the permission of type 'System.Net.WebPermission, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

    how can i resolve it !!!!

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: call Rest web service from plugin c#

    Compare the .net versions between the plugin and console app.  Also make sure you aren't using any assemblies that aren't available in the crm environment (my experience is only with CRM online and we have to be careful about the assemblies we use - not sure if this is the case in on-premise).

  • Amarsen Profile Picture
    Amarsen 55 on at
    RE: call Rest web service from plugin c#

    You can enable plugin trace log and capture the actual error message thrown by the plugins. This is very useful for tracing issues within plugins and custom workflows in development env. but it effects performance so advise you to disable the tracing as you complete it.

    Also, try to use plugin tracing (ITracingService) so that you can exactly see where it is failing.

    Try this code

    -------------

    string returnValue = string.Empty;

    HttpWebRequest request;

    WebResponse response;

    try

    {

    request = (HttpWebRequest)WebRequest.Create(baseUrl);

    request.Timeout = 100000;

    byte[] data = Encoding.UTF8.GetBytes(message);

    request.Method = Method.ToString();

    request.ContentType = ContentType;

    Stream dataStream = request.GetRequestStream();

    dataStream.Write(data, 0, data.Length);

    dataStream.Close();

    response = request.GetResponse();

    if(response!=null)

    {

    returnValue = new StreamReader(response.GetResponseStream()).ReadToEnd();

    }

    }

    catch (Exception ex)

    {

    returnValue = ex.Message;

    if (ex is WebException)

    {

    var webException = new StreamReader(((WebException)ex).Response.GetResponseStream()).ReadToEnd();

    returnValue = returnValue + webException;

    }

    }

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: call Rest web service from plugin c#

    I do it /run as different user and i set the user cridential crm but i get the some issue ...

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: call Rest web service from plugin c#

    You won't be able to step through the web service code when debugging the plugin with the PluginRegistration tool (it's a replayer, not a true debugger so it fails when it hits the web service code).

    I'm using CRM Online and we've created a custom log entity to log plugin exceptions.  Not familiar with onpremise, but can you log the full error information (or exception info)?

    Also, what user security context does the plugin run under (double-click the step info to see the "run in user's context" account).  In your console app, can you attempt to debug your console app using the same user?  You can run Visual Studio as a different user by holding down the shift key and right clicking on the Visual Studio icon, then choose "run as different user".

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: call Rest web service from plugin c#

    hi Francesco

    firstly i use on premise instance of crm and i try to call a external REST web service from this plugin

    tnx

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: call Rest web service from plugin c#

    Hi ghazi,

    please refer to the following page to solve your problem:

    https://github.com/jlattimer/CrmWebApiCSharp 

    Please let me know if you solve.

    If you found the answer helpful, please mark as Verified 

    Join my network on LinkedIn      Follow me on Twitter 

    Thank You & Best Regards

    Francesco Picchi

    Microsoft Dynamics CRM Consultant, Bologna+Milano, ITALY

    Independent Contractor

    http://www.francescopicchi.com

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans