gpt4 book ai didi

.net - 如何以编程方式在同一 URL 上创建 WCF 服务及其元数据

转载 作者:行者123 更新时间:2023-12-04 11:30:14 24 4
gpt4 key购买 nike

TL;博士
我会在同一个 URL 上运行所有三件事(“您已经创建了一个 Web 服务”页面、WSDL 页面和实际的 Web 服务),类似于在独立 WebService 应用程序中创建的 WCF 服务项目。

我正在以编程方式创建 WCF 端点并将其中大部分放在一起。最后一件事是我无法让元数据 URL 与服务 URL 相同。我知道这应该是可能的,因为您可以从 Visual Studio 创建这样的服务。

发生的情况是我可以在浏览器中浏览 WSDL,我可以将它添加为 Web 引用,但我无法从新创建的项目中调用它。如果我同时删除友好页面和 wsdl 页面,我就可以调用该服务。

下面是我正在使用的代码。

class Program
{
private static ManualResetEvent _ResetEvent = new ManualResetEvent(false);

static void Main(string[] args)
{
Console.TreatControlCAsInput = true;

var serviceUrl = "Fibonacci.svc";
new Thread(() =>
{
var baseUri = new Uri("http://ws.test.com");
var serviceUri = new Uri(baseUri, serviceUrl);
BasicHttpBinding binding = new BasicHttpBinding();
using (var host = new ServiceHost(typeof(Fibonacci), new[] { baseUri }))
{
host.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpGetEnabled = true, HttpGetUrl = new Uri(baseUri, serviceUrl) });
host.Description.Behaviors.Find<ServiceDebugBehavior>().IncludeExceptionDetailInFaults = true;
host.Description.Behaviors.Find<ServiceDebugBehavior>().HttpHelpPageUrl = serviceUri;

host.AddServiceEndpoint(typeof(IFibonacci), binding, serviceUri);

Console.WriteLine("Started service on cotnract {0}, ready for anything", typeof(IFibonacci).FullName);

host.Open();
_ResetEvent.WaitOne();
}
}).Start();



while (true)
{
var cki = Console.ReadKey(true);
if (cki.Key == ConsoleKey.C && (cki.Modifiers & ConsoleModifiers.Control) != 0)
{
_ResetEvent.Set();
break;
}
}

}
}

最佳答案

事实证明,解决方案很简单。 documentation for HttpGetUrl有点迟钝,基本上为了让所有三个都使用相同的 URL,您需要创建 ServiceHost完整网址 服务,然后只设置 ServiceMetaBaseBehaviour.HttpGetEnabletrue .

相关代码如下。

var baseUri = new Uri("http://ws.test.com");
var serviceUri = new Uri(baseUri, serviceUrl);
BasicHttpBinding binding = new BasicHttpBinding();
using (var host = new ServiceHost(typeof(Fibonacci), serviceUri /*Specify full URL here*/))
{
host.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpGetEnabled = true /*Do not specify URL at all*/});
host.Description.Behaviors.Find<ServiceDebugBehavior>().IncludeExceptionDetailInFaults = true;
host.Description.Behaviors.Find<ServiceDebugBehavior>().HttpHelpPageUrl = serviceUri;

host.AddServiceEndpoint(typeof(IFibonacci), binding, string.Empty /*Url here can either be empty or the same one as serviceUri*/);

Console.WriteLine("Started service on cotnract {0}, ready for anything", typeof(IFibonacci).FullName);

host.Open();
_ResetEvent.WaitOne();
}

关于.net - 如何以编程方式在同一 URL 上创建 WCF 服务及其元数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5907791/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com