Consume WCF Soap Service Csharp

Consume WCF Soap Service Csharp

a. Add a service reference in your project .

add_service

 

Corresponding code will appear in app.config file:

<?xml version=”1.0″?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name=”basicHttp” />
</basicHttpBinding>
<wsHttpBinding>
<binding name=”WSHttpBinding_IAdoptorServiceManger” messageEncoding=”Mtom” >
<security mode=”Transport”>
<transport clientCredentialType=”None” />
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address=”http://www.bank.co.in/TU.IDS.ExternalServices_UAT/SolutionExecution/ExternalSolutionExecution.svc”
binding=”basicHttpBinding” bindingConfiguration=”basicHttp”
contract=”bankServiceReference1.IExternalSolutionExecution”
name=”basicHttp” />
<endpoint address=”https://www.bank.co.in/DC/ICICI_POS_Wrapper/ICICIWrapper.svc”
binding=”wsHttpBinding” bindingConfiguration=”WSHttpBinding_IAdoptorServiceManger”
contract=”ServiceReference2.IAdoptorServiceManger” name=”WSHttpBinding_IAdoptorServiceManger” />
</client>
</system.serviceModel>
<startup><supportedRuntime version=”v4.0″ sku=”.NETFramework,Version=v4.0″/></startup></configuration>

Here in above example , I have shown two types of code generation , one for :

a. WSHttpBinding

WSHttpBinding b = null;
string strUrl1 = “”;
if (strUrl.Length > 0)
{
strUrl1 = strUrl.ToLower();
bool bIshttps = strUrl1.StartsWith(“https”);
if (bIshttps)
{

b = new WSHttpBinding(SecurityMode.Transport);
}
else
{
bool bIshttp = strUrl1.StartsWith(“http”);
if (bIshttp)
{
b = new WSHttpBinding(SecurityMode.None);
}
else
{
RespMessage = “INVALID URL”;
ResponseCode = “101”;
return;
}
}
}
else
{
RespMessage = “INVALID URL”;
ResponseCode = “101”;
return;
}

TimeSpan timeSpan = new TimeSpan(0, 0, 0, 0, iTimeOut);

b.ReceiveTimeout = timeSpan;
var remoteadd = new System.ServiceModel.EndpointAddress(strUrl); //strURL is .svc URL path
b.MessageEncoding = WSMessageEncoding.Mtom;

ServiceReference2.IAdoptorServiceManger objICICI = null;

obj= new ServiceReference2.AdoptorServiceMangerClient(b, remoteadd);

string strresponse = obj.ApiCall(strReq);

Creation of Request class:

Get the request xml :

<Request>  <Attr1>DEBE4BB4072169156CFA525DD4273316A0239A99F90DFE2B20C737F1D7FB3 ABB</Attr1>   <Attr2>500000</Attr2>   <Attr3>24</Attr3>     <Attr4>PL</Attr4> </Request>

and create .xsd file using link: http://xmlgrid.net/xml2xsd.html

Copy created xsd content:

<xs:schema xmlns:xs=”http://www.w3.org/2001/XMLSchemaelementFormDefault=”qualifiedattributeFormDefault=”unqualified>

          <!– XML Schema Generated from XML Document on Fri Sep 23 2016 17:24:38 GMT+0530 (India Standard Time) –>
          <!– with XmlGrid.net Free Online Service http://xmlgrid.net –>

       <xs:element name=”Request>

              <xs:complexType>

                     <xs:sequence>

                            <xs:element name=”Attr1type=”xs:string></xs:element>
                            <xs:element name=”Attr2type=”xs:int></xs:element>
                            <xs:element name=”Attr3type=”xs:int></xs:element>
                        </xs:sequence>
                 </xs:complexType>
          </xs:element>
   </xs:schema>
Create .xsd file using above content.
Generate C# file using this xsd file:
Open Visual Studio cmd prompt in Admin mode.
type following:
xsd /c [Name].xsd
Add created .cs and .xsd file in your c# project.
Use the object to create the reqest.
Request req= new Request();
req.Attrib1 = “DEBE4BB4072169156CFA525DD4273316A0239A99F90DFE2B20C737F1D7FB3ABB”;
req.Attrib2 = 500000;
req.Attrib3=”AL”;
req.Attrib4=23;
StringWriter textWriter1 = new StringWriter();
XmlSerializer XMLSerializationObject = new XmlSerializer(req.GetType());writer = XmlWriter.Create(textWriter1, settings);

XmlSerializerNamespaces objNameSpace = new XmlSerializerNamespaces();
objNameSpace.Add(“”, “http://bànk.com/dc/extsvc”);

XMLSerializationObject.Serialize(writer, req, objNameSpace);
string csXML = textWriter1.ToString();
string postData = “”;
postData = csXML;

 string strresponse = obj.ApiCall(strReq);
Similarly create response class and use it to parse the response from response xml creating .xsd file and generating c# class.

b. BasicHTTPBinding

BasicHttpBinding b = null;
string strUrl1 = “”;
if (strUrl.Length > 0)
{
strUrl1 = strUrl.ToLower();
bool bIshttps = strUrl1.StartsWith(“https”);
if (bIshttps)
{

b = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
}
else
{
bool bIshttp = strUrl1.StartsWith(“http”);
if (bIshttp)
{
b = new BasicHttpBinding(BasicHttpSecurityMode.None);
}
else
{
RespMessage = “INVALID URL”;
ResponseCode = “101”;
return;
}
}
}
else
{
RespMessage = “INVALID URL”;
ResponseCode = “101”;
return;
}
TimeSpan timeSpan = new TimeSpan(0, 0, 0, 0, iTimeOut);

b.ReceiveTimeout = timeSpan;
var remoteadd = new System.ServiceModel.EndpointAddress(strUrl); // .svc path is strUrl

obj = new ServiceReference1.ApiCall(b, remoteadd);

 

Exceptions that you may get :

a. The content type multipart/related; type=”application/xop+xml”;start=”<http://tempuri.org/0>”;boundary=”uuid:7f9e7a61-ae71-4370-a703-03cc0e816be8+id=44″;start-info=”application/soap+xml” of the response message does not match the content type of the binding (application/soap+xml; charset=utf-8). If using a custom encoder, be sure that the IsContentTypeSupported method is implemented properly. The first 932 bytes of the response were: ‘
–uuid:7f9e7a61-ae71-4370-a703-03cc0e816be8+id=44
Content-ID: <http://tempuri.org/0>
Content-Transfer-Encoding: 8bit
Content-Type: application/xop+xml;charset=utf-8;type=”application/soap+xml”

Solution: 

Make sure , you have not made changes in bindings, contract and endpoints generated in app.config on addittion of service reference.

Add encoding as Mtom , as shown above in code for WSHttpbinding:

b.MessageEncoding = WSMessageEncoding.Mtom;

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *