Get rid of the dependence on Servicereferences.clientconfig

Source: Internet
Author: User
Tags silverlight

In Silverlight, if you add a reference to a WCF Service with VS, the Servicereferences.clientconfig configuration file is automatically generated, containing the Binding and Address of the service, etc. Information. Isolating the configuration information would have been a good thing, but the problem is that because Silverlight is only a feature of the client runtime, the configuration file will be assembled into the Siverlight XAP package at compile time, so modifying the configuration becomes cumbersome, Re-compile and redeploy each time you want to modify it. The config file generated by VS often contains a direct reference to the address where the Service is located. such as HTTP://LOCALHOST/SL_SERVICE/SERVICE.SVC, it is very inconvenient for us to deploy to the production environment.

In other case, if we can place the Silverlight-hosted page with the WCF Service on the same site, then you can use the relative address to access the Service. Migrating between the development environment/test environment/production environment can be very convenient.

Where ClientBin is the XAP package that compiles the generated Silverlight program.

Based on this structure, we can do a serviceclientfactory class that can create a WCF client proxy class of the specified type on demand without having to read the configuration file. The code is as follows:

Using System;
Using System.ServiceModel;
Using System.ServiceModel.Channels;
Using System.Windows;
Using Sl_servicemanage. Serviceref;
Namespace Sl_servicemanage
{
PublicClass Servicemanger
{
======================= Method One ======================================
PublicStatic ServiceClient Getdynamicclient (ParamsString[] svcnames)
{
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;
String path ="/sl_service/service.svc";
if (Svcnames.length > 0)
{
Path ="/sl_service/" + svcnames[0];
}
ReturnNew ServiceClient (binding,New EndpointAddress (New Uri (Application.Current.Host.Source, Path));
}

}
======================= Method Two ======================================
PublicStaticClass Serviceclientfactory<tserviceclient, tservice>
where Tserviceclient:clientbase<tservice>, TService
where TService:Class
{
PublicStatic Tserviceclient createserviceclient ()
{
var typeName =typeof (TService). Name;
var serviceaddress ="/sl_service/" + TypeName +". Svc";
var endpointaddr =New EndpointAddress (New Uri (Application.Current.Host.Source, serviceaddress));
var binding =New BasicHttpBinding ();
Binding. MaxReceivedMessageSize =int. MaxValue;
            binding. Maxbuffersize = int. MaxValue;
            var ctor =  typeof (tserviceclient). GetConstructor (new type[] { typeof (Binding),  typeof (EndpointAddress)  });
             return  (Tserviceclient) ctor. Invoke (new OBJECT[]&NBSP;{&NBSP;BINDING,&NBSP;ENDPOINTADDR&NBSP;});
        }
    }

This allows you to create a client proxy using code similar to the following:

//serviceclient client = servicemanger.getdynamicclient ("Service.svc");
ServiceClient client = Serviceclientfactory<serviceclient, Service>. Createserviceclient ();
Client. doworkcompleted + = new eventhandler<doworkcompletedeventargs> (client_doworkcompleted);
Client. Doworkasync ();

There are more than two types of parameters that are created directly from the new method, but do not need to be dependent on the configuration file.

Get rid of the dependence on Servicereferences.clientconfig

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.