【发布时间】:2014-11-07 01:11:02
【问题描述】:
我已经研究了一天,在这里向各位专家寻求建议。
我有 REST 服务,它是主要的 IIS 托管服务。
此服务需要与 32 位非托管 .dll 接口。为了做到这一点,我采用了一种常见的方法,即创建一个自托管服务并对其进行调用。
在这样的背景下,我有一段时间让自托管的主机正常工作。
这是有效的代码:
static void Main(string[] args)
{
Uri baseAddress = new Uri("http://localhost:8080/theSvc");
Uri mexUri = new Uri("http://localhost:8080/theSvc/mex");
// Create the ServiceHost.
using (ServiceHost host = new ServiceHost(typeof(ImgService), baseAddress))
{
// var wsHttpBinding = new WSHttpBinding();
// wsHttpBinding.MaxReceivedMessageSize = int.MaxValue;
// host.AddServiceEndpoint(typeof(IImgService), wsHttpBinding, baseAddress);
// Enable metadata publishing.
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetUrl = mexUri;
smb.HttpGetEnabled = true;
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
host.Description.Behaviors.Add(smb);
// Open the ServiceHost to start listening for messages. Since
// no endpoints are explicitly configured, the runtime will create
// one endpoint per base address for each service contract implemented
// by the service.
host.Open();
Console.WriteLine("The service is ready at {0}", baseAddress);
Console.WriteLine("Press <Enter> to stop the service.");
Console.ReadLine();
// Close the ServiceHost.
host.Close();
}
}
}
现在...如果我取消注释这些行以增加 maxreceivedMessageSize 并从 using 语句中删除 baseAddress,我将无法再添加对服务的引用:
所以改变这个:
using (ServiceHost host = new ServiceHost(typeof(ImgService), baseAddress))
到这里:
using (ServiceHost host = new ServiceHost(typeof(ImgService)))
并取消注释:
// var wsHttpBinding = new WSHttpBinding();
// wsHttpBinding.MaxReceivedMessageSize = int.MaxValue;
// host.AddServiceEndpoint(typeof(IImgService), wsHttpBinding, baseAddress);
我收到的错误是:
... -> 本地主机
There was an error downloading 'http://...:8080/theSvc/_vti_bin/ListData.svc/$metadata'.
The request failed with HTTP status 400: Bad Request.
Metadata contains a reference that cannot be resolved: 'http://...:8080/theSvc'.
Metadata contains a reference that cannot be resolved: 'http://...:8080/theSvc'.
If the service is defined in the current solution, try building the solution and adding the service reference again.
就像其他在这里发帖的人一样……我陷入了困境。非常感谢任何帮助。
【问题讨论】:
-
您是否考虑过 WCF 的替代方法,与简单的 HTTP 服务相比,这种方法的设置往往比较复杂?
-
如果能满足我的需求,我会 100% 全力以赴。如果我能弄清楚最后一个配置部分,我觉得我与 WCF 服务非常接近
-
好吧,你已经被警告了 :) 说真的,我帮不了你,我小心避免 WCF
-
纯 HTTP 和 NancyFx 甚至纯 HttpListener 都可以,除非你需要 WCF 功能。
-
如果你包装的 dll 需要暴露的只是一个字符串,你不太可能需要 WCF,但这并不意味着你不能使用它。看看你是否让它工作或查看我提到的两个选项。
标签: c# web-services wcf self-hosting