Using SOAP Web Services in Visual Studio. Add Service Reference vs Add Web Reference

1 minute read time.

Within a Visual Studio Project there are two ways of referencing Web Services. These are either as a Service Reference or a Web Reference.

  • Add Web Reference is the old-style, and is more commonly used within Sage CRM projects. This is the type of reference that is used with older ASP.NET (ASMX) examples that used only XmlSerializer. A Web Reference will create a client proxy class that allows your code to talk to the Sage CRM Web Service that is described via WSDL. The communication is via SOAP and using a Web References will allow Sage CRM to talk to anything so long as they all talk WSDL and conform to the WS-I interoperability standard.
  • Add Service Reference is the "newer" way of doing it. "Newer" is a relative term. Visual Studio added support for Service References along with with .NET 3.5 in Visual Studio 2008. Add Service allows you to add a Windows Communication Foundation (WCF) service reference to your project. The WCF approach allows for a much more advanced, much more flexible service model than the earlier Web Reference. The down side is that you end up having to write more code when using a Service Reference against Sage CRM.

The examples provided in the Sage CRM documentation, SDK snippets and community resources assume you are working with a Web Reference for your project.

The example below shows the code of a project that has used a service reference.

The project when run will create a logon button. The logon button when pressed will then logon to Sage CRM, retrieve the version of Sage CRM and then will log out.

Example Code

//Create the Web Service & Logon
WebServiceSoapPort CRMService = new WebServiceSoapPortClient();

logonRequest myLogonRequest = new logonRequest("Admin", "");
logonResponse myLogonResponse = CRMService.logon(myLogonRequest);

SessionHeader crmSessionHeader = new SessionHeader();
crmSessionHeader.sessionId = myLogonResponse.result.sessionid;

//Send a request
getversionstringRequest myVersionRequest = new getversionstringRequest();
myVersionRequest.SessionHeader = crmSessionHeader;
myVersionRequest.getversionstring = new object();
getversionstringResponse1 myVersionResponse = CRMService.getversionstring(myVersionRequest);

//Use the returned result
buttonLogon.Text = myVersionResponse.getversionstringresponse.result.versionstring;

//Logoff
logoffRequest myLogoffRequest = new logoffRequest(crmSessionHeader, crmSessionHeader.sessionId);
logoffResponse myLogoffResponse = CRMService.logoff(myLogoffRequest);

MessageBox.Show("User logged off");