Partager via


WCF ServiceHost Custom Config from AppSettings

hi All the code snippet below would create a simple service host loaded from a normal app.config instead of having to understand the complex WCF service configuration

this is how we would initiate the servicehost..

MyFramework.ServiceModel.WCFService<

IHelloWorld, HelloWorldImpl> sh = new WCFService<IHelloWorld, HelloWorldImpl>();

this is how the Servicehost override would look like ..

using

System;

using

System.Collections.Generic;

using

System.ServiceModel;

using

System.ServiceModel.Description;

using

System.Configuration;

using

System.ServiceModel.Channels;

namespace

MYFramework.ServiceModel

{

public class WCFService<InterFaceType, TypeImpl> : ServiceHost

where InterFaceType : class

where TypeImpl : class

{

protected EndpointAddress myEndpoint;

protected Binding myBinding;

public WCFService()

:

base(typeof(TypeImpl))

{

InitializeFromConfig();

}

protected void InitializeFromConfig()

{

//

// Initialize the configuration section.

//

// Create a channel factory.

switch (System.Configuration.ConfigurationManager.AppSettings["binding"] as string)

{

case "NetTcpBinding":

myBinding =

new NetTcpBinding();

break;

case "BasicHttpBinding":

myBinding =

new BasicHttpBinding();

break;

case "WSHttpBinding":

myBinding =

new WSHttpBinding();

break;

default:

myBinding =

new BasicHttpBinding();

break;

}

myEndpoint =

new EndpointAddress(System.Configuration.ConfigurationManager.AppSettings["Url"]);

this.AddServiceEndpoint(typeof(InterFaceType), myBinding, myEndpoint.Uri);

}

}

}