Consume WCF Soap Service Csharp
a. Add a service reference in your project .
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/XMLSchema” elementFormDefault=”qualified” attributeFormDefault=”unqualified“>
<xs:element name=”Request“>
<xs:complexType>
<xs:sequence>
req.Attrib1 = “DEBE4BB4072169156CFA525DD4273316A0239A99F90DFE2B20C737F1D7FB3ABB”;
req.Attrib2 = 500000;
req.Attrib3=”AL”;
req.Attrib4=23;
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;
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;
Resolving technical problems:
Solve your technical problems instantly
We provide Remote Technical Support from Monday to Sunday, 7:00PM to 1:00 AM
Mail your problem details at [email protected] along with your mobile numberand we will give you a call for further details. We usually attend your problems within 60 minutes and solve it in maximum 2 days.