Consume Web-request from Plugin

web

Generally, CRM not works alone, it includes many other application, such time we needs to move data from one application to another. In this situation we needs integration; We do integration with either “HttpWebRequest” or “BasicHttpBinding”. 

Explore “HttpWebRequest” implementation here .

To consume web-service using “BasicHttpBinding”, I used following way.

  1. Created a proxy class for web request
  2. Consumed service

To create proxy class, I used ‘svcutil.exe’ utility. Follow following steps to create proxy class

  1. Open Visual Studio’s command prompt (ref image 1 below), also it will work as expected on normal command prompt
  2. Execute ‘exe /language:cs /out:generatedProxy.cs your WSDL service URL’ command. Ref Image 2 below

It will create proxy class and will be stored your local machine. (In my case it was ‘Test’ folder). Ref below screen shot…

Image 1:

cmd1

Image 2:

cmd-2

Now, here is the code sample to execute web-request.

private static XDocument BuildRequest(string url, string requestStr, Entity Config)
 {
 //requestStr = testRequest();
 XmlDocument xmlDoc = new XmlDocument();
 xmlDoc.LoadXml(requestStr);

 BasicHttpBinding basichttpBinding = new BasicHttpBinding();
 basichttpBinding.Name = "BasicHttpBinding_IPOCService";
 basichttpBinding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
 basichttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
 basichttpBinding.MaxBufferSize = 22065536;
 basichttpBinding.MaxReceivedMessageSize = 22065536;
 basichttpBinding.TransferMode = TransferMode.Buffered;

 EndpointAddress endPointAddress = new EndpointAddress(url);
//Below code will be as per web service 
 ServicesClient client = new ServicesClient(basichttpBinding, endPointAddress);

 EncryptPassword enp = new EncryptPassword();

 client.ClientCredentials.Windows.ClientCredential.Domain = Config.Contains("soft_domain") ? Config.Attributes["soft_domain"].ToString() : "";
 client.ClientCredentials.Windows.ClientCredential.UserName = Config.Contains("soft_namespace") ? Config.Attributes["new_namespace"].ToString() : "";
 client.ClientCredentials.Windows.ClientCredential.Password = Config.Contains("soft_passcode") ? enp.Decrypt(Config.Attributes["soft_passcode"].ToString()) : "";


 XDocument xDoc = null;
 //Cursor.Current = Cursors.WaitCursor;
 if (xmlDoc.DocumentElement.LocalName == "S****Request")
 {
 S****Request synchRequest = new SynchRequest();
 s****Request = XmlDeserialize<S****Request>(requestStr, "S****Request", "http://schemas.munichre.com/eai/ws/DocProdServices/0.9.20151021/GenerateSynch");
 S****Response response = client.GenerateS****(synchRequest);

 xDoc = XDocument.Parse(XmlSerialize<S****Response>(response));
 }
 else if (xmlDoc.DocumentElement.LocalName == "S****StoreRequest")
 {
 S****StoreRequest s****StoreRequest = new S****StoreRequest();
 s****StoreRequest = XmlDeserialize<S****StoreRequest>(requestStr, xmlDoc.DocumentElement.LocalName, xmlDoc.DocumentElement.NamespaceURI);
 S****StoreResponse response = client.GenerateS****Store(s****StoreRequest);
 xDoc = XDocument.Parse(XmlSerialize<S****StoreResponse>(response));
 }
 else
 {
 xDoc = null;
 throw new Exception("Invalid Request.");
 }

 return xDoc;
 }

Thanks…

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s