C# WCF 服务,如何从类库中获取网站 URL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1822774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 21:07:55  来源:igfitidea点击:

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

c#.netwcf

提问by JL.

I have a WCF service running in IIS that calls a function in a class library where httpContext is available. How can I dynamically get the web site url, this may also be a virtual directory?

我有一个在 IIS 中运行的 WCF 服务,它调用 httpContext 可用的类库中的函数。如何动态获取网站url,这也可能是一个虚拟目录?

采纳答案by Russell

You could create a ServiceHostFactory which launches your service host manually, then store the endpoint address in a static class to be used by your application. Here is a simple example:

您可以创建一个 ServiceHostFactory 手动启动您的服务主机,然后将端点地址存储在您的应用程序要使用的静态类中。这是一个简单的例子:

(in your myService.svc):

(在您的 myService.svc 中):

<%
 @ServiceHost
 Service="MyNamespace.MyService" 
 Factory="MyNamespace.MyServiceHostFactory"
  %>

(in your MyServiceHostFactory.cs):

(在您的 MyServiceHostFactory.cs 中):

/// <summary>
/// Extends ServiceHostFactory to allow ServiceHostFactory to be used.
/// </summary>
public class MyServiceHostFactory : ServiceHostFactory
{
    /// <summary>
    /// Creates a new ServiceHost using the specified service and base addresses.
    /// </summary>
    /// <param name="serviceType"></param>
    /// <param name="baseAddresses"></param>
    /// <returns></returns>
    protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
    {
        ServiceHost host;
        host = new ServiceHost(serviceType, baseAddresses);

        MyGlobalStaticClass.Address = baseAddresses[0]; // assuming you want the first endpoint address.

        return host;
    }

(In your MyGlobalStaticClass.cs):

(在您的 MyGlobalStaticClass.cs 中):

  public static string Address = "";

回答by tjmoore

I'm not too hot on WCF as I'm more used to .Net 2.0, but would this do it?

我不太喜欢 WCF,因为我更习惯于 .Net 2.0,但是这样做可以吗?

HttpContext.Current.Request.Url.ToString()

That should give you the url of the calling request. The catch here is that you could possibly have multiple domains or virtual directories pointing to the same service and it will only give you the url the client specified. However if you have multiple entry points, there is no "one" url anyway.

这应该为您提供调用请求的网址。这里的问题是您可能有多个域或虚拟目录指向同一个服务,它只会为您提供客户端指定的 url。但是,如果您有多个入口点,则无论如何都没有“一个”网址。

回答by Ajaxx

I'm going to start by assuming that you're using HTTP - I'm sure you can adjust the approach depending on what your specific conditions dictate. I tried to get an answer using HttpContext as well and found out that the value was null when running under Cassini so I tried an alternative approach.

我将首先假设您正在使用 HTTP - 我相信您可以根据您的特定条件来调整方法。我也尝试使用 HttpContext 获得答案,发现在 Cassini 下运行时该值为 null,因此我尝试了另一种方法。

System.ServiceModel.OperationContext contains the proper request context. You can follow the request down to the actual request message and scrub the header.

System.ServiceModel.OperationContext 包含正确的请求上下文。您可以跟踪请求到实际的请求消息并清理标头。

Uri requestUri = System.ServiceModel.OperationContext.Current.RequestContext.RequestMessage.Headers.To;

回答by Sanjay Rathod

Currently I am working on WCF REST Service and I have same kind of requirement. I need service Host URL in my one of method. Here below is the different ways to get WCF REST Service Host/URL in class library.

目前我正在研究 WCF REST 服务,我也有同样的需求。我的方法之一需要服务主机 URL。下面是在类库中获取 WCF REST 服务主机/URL 的不同方法。

You can use WebOperationContextclass which availables in System.ServiceModel.Webnamespace to getting service url. Please note this class is only for WCF REST Service.

您可以使用命名空间WebOperationContext中可用的类System.ServiceModel.Web来获取服务 url。请注意,此类仅适用于 WCF REST 服务。

  1. WebOperationContext.Current.IncomingRequest.Headers["host"]- Gives Service Host Name

  2. WebOperationContext.Current.IncomingRequest.UriTemplateMatch.BaseUri.Host- Gives Service Host Name

  3. WebOperationContext.Current.IncomingRequest.UriTemplateMatch.BaseUri.AbsoluteUri- Gives Service Full Url

  4. WebOperationContext.Current.IncomingRequest.UriTemplateMatch.RequestUri.AbsoluteUri- Gives Service Full Url

  1. WebOperationContext.Current.IncomingRequest.Headers["host"]- 提供服务主机名

  2. WebOperationContext.Current.IncomingRequest.UriTemplateMatch.BaseUri.Host- 提供服务主机名

  3. WebOperationContext.Current.IncomingRequest.UriTemplateMatch.BaseUri.AbsoluteUri- 提供服务完整网址

  4. WebOperationContext.Current.IncomingRequest.UriTemplateMatch.RequestUri.AbsoluteUri- 提供服务完整网址

You can get more information about WebOperationContextclass on MSDN

您可以WebOperationContextMSDN上获取有关类的更多信息