When using WCF or WS for database operations, you need to have a clear service address, if WCF is the Svc file address. Beginners usually add a reference directly using the Add Service reference of VS. This is indeed a very convenient means, but what kind of trouble will it bring?
First, once the directory structure of the WS file has changed in the development phase, the WS address in SL project must be changed, and this change is not simply the update Service reference, because the address has changed, You have to delete the old reference to add the new reference, which is one.
Second, the big problem comes when project development is done to deploy the server. If you add a reference using the VS Add Service reference at development time, the system automatically generates a Servicereferences.clientconfig configuration file with information such as the address and cache of WS and the length of the connection. It's killing me. This configuration file is packaged into the XAP file with SL project, which means that once the project is deployed to the server, it cannot be changed to the address of WS. This has a very negative impact on the migration of the project and the change of the server IP address.
Here is a way to dynamically obtain a WS address, which does not depend on the Servicereferences.clientconfig file. Although not dependent on configuration files, developers still need to use the VS Add Service reference to add a reference to generate the SL-side operational code. Okay, let's get down to business. The following is the class used to generate WSClient
Copy Code code as follows:
public class Serviceutil
{
public static string Svcpath {set; get;}
<summary>
Get Data Service Path
</summary>
<returns>path</returns>
public static workflowserviceclient Getdynamicclient ()
{
if (Svcpath = null)
throw new Exception ("Wrong SVC path!");
BasicHttpBinding binding = new BasicHttpBinding (Application.Current.Host.Source.Scheme.Equals ("https", stringcomparison.invariantcultureignorecase)? BasicHttpSecurityMode.Transport:BasicHttpSecurityMode.None);
Binding. maxreceivedmessagesize = Int. MaxValue;
Binding. MaxBufferSize = Int. MaxValue;
return new Workflowserviceclient (binding, New EndpointAddress (New Uri (Application.Current.Host.Source, Svcpath));
}
}
The above Serviceutil class has a static string variable to hold the WS file path, and the following Getdynamicclient method combines the host site URI of the SL application with the WS file path to form the complete WS reference URL. This static variable svcpath is usually assigned before using the WS operation. and the specific value can be passed through the site Web.config file appsetting in the field, as to how the SL project to get the parameters of the ASPX page, I wrote in the last essay, we can refer to.