Thursday 15 April 2010

c# - WCF Service, how to get the web site URL from within a class library? -


I have a WCF service running in IIS, which functions in a class library where httpContext is available. How can I get a web site URL dynamically, it can also be a virtual directory?

You can create a ServiceHostFactory that launches your service host manually, then your application Here is a simple example:

(in your myService.svc):

  & lt;% @, store the endpoint address in a static category used by Servicehost service = "MyNamespace.MyService" factory = "MyNamespace.MyServiceHostFactory" & gt%;  

(in your MyServiceHostFactory.cs):

  /// & lt; Summary & gt; Increases ServiceHostFactory to allow access to /// ServiceHostFactory. /// & lt; / Summary & gt; Public category MyServiceHostFactory: ServiceHostFactory {/// & lt; Summary & gt; /// Creates a new ServiceHost using the specified service and base address /// & lt; / Summary & gt; /// & lt; Param name = "service type" & gt; & Lt; / Param & gt; /// & lt; Param name = "baseAddresses" & gt; & Lt; / Param & gt; /// & lt; Returns & gt; & Lt; / Returns & gt; Protected Override ServiceHost CreateServiceHost (Type Service Type, URI [] baseAddresses) {ServiceHost host; Host = new service host (service type, baseadress); MyGlobalStaticClass.Address = baseAddresses [0]; // Assume that you want to know the endpoint first; return host; }  

(in your MyGlobalStaticClass.cs):

  public static string address = "";  

No comments:

Post a Comment