Skip to main content

Notifications

Dynamics 365 general forum

Unable to consume a WCF Service from a Plugin(Dynamic CRM 365 online)

Posted on by 20

Hi All

I am working on Dynamics 365 Online..I'm facing a Permission error when i try to call an external Azure WebService from my plugin.
The code I wrote for that works fine in a console application, but it fails in the plugin. The code looks like the following:
error: 400 (Bad Request)


string keyName = "MyKey";
string key = "V2333344YUzgyXP83cIBgfYqFo=";

var url = "myazure.servicebus.windows.net/.../CBD06A18-0CF9-E811-A97B-000D3AA04914";
var token = GetToken(url, keyName, key);
var response = GetResultFromService(url, token);


private HttpResponseMessage GetResultFromService(string url, string token)
{

var httpClient = new HttpClient();

httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", token);
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
httpClient.Timeout = new TimeSpan(0, 10, 30);
var response = httpClient.PutAsync(url,null).Result;

var responseText = response.Content.ReadAsStringAsync().Result;
return response;

}


public string GetToken(string resourceUri, string keyName, string key)
{
var expiry = GetExpiry();
string stringToSign = WebUtility.UrlEncode(resourceUri) + "\n" + expiry;
HMACSHA256 hmac = new HMACSHA256(Encoding.UTF8.GetBytes(key));

var signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));
var sasToken = String.Format(CultureInfo.InvariantCulture, "SharedAccessSignature sr={0}&sig={1}&se={2}&skn={3}",
WebUtility.UrlEncode(resourceUri), WebUtility.UrlEncode(signature), expiry, keyName);


return sasToken;
}

Any help?

Thanks

BV

  • Biju Profile Picture
    Biju 20 on at
    RE: Unable to consume a WCF Service from a Plugin(Dynamic CRM 365 online)

    Thanks for your help Johnny. Same code works in the CRM 2015 (on premises version) and console application.

    I also tried simplest way like this one:

    using (WebClient wc = new WebClient())

    {

       wc.Headers.Add("Authorization: Bearer ey8927eg534gnm89u24xcm4856454c5t....."); // If you need auth

       string output = wc.DownloadString("api.examplewebservice.com/customers");

    }

    I hope anyone can help me out with this.

  • Johnny Gong Profile Picture
    Johnny Gong 6,482 on at
    RE: Unable to consume a WCF Service from a Plugin(Dynamic CRM 365 online)

    Hi Biju,

    400 is a common HTTP response codes which means server was unable to process the request sent by the client due to invalid syntax

    I would suggest you to refer to the general rules for trying to access an external webservice from a plug-in from the following post:  

    community.dynamics.com/.../253009

    Or you could raise service request to get the corresponded D365 Online trace logs or add the ITracingService in your program for debugging.

    docs.microsoft.com/.../gg509030(v=crm.8)

    www.cloudfronts.com/call-web-service-plugin-dynamics-crm

    Regards

    Johnny

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans