我需要创建一个 WCF 服务,该服务位于服务器(托管在 IIS 中)上,并侦听在某个 URL 上访问服务器的任何 HTTP GET 请求。
更具体地说,当有效的 GET 请求到达服务器时,我想检查 URL 中编码的参数。
以下是我的界面的代码:
[ServiceContract]
public interface IHttpHandler
{
[OperationContract(Action = "*", ReplyAction = "*")]
void HandleMessage(Message m);
[OperationContract]
[WebInvoke(UriTemplate = "HandleEchoRequest", Method = "GET")]
string HandleEchoRequest(Stream request);
}
实现代码(svc.cs文件):
public void HandleMessage(Message m)
{
//do some work
}
public string HandleEchoRequest(Stream request)
{
//...do something here
return "asdf";
}
我可以成功命中 SVC:
http://localhost/HttpMessageProcessor/HttpHandler.svc
但是,当附加到 W3WP 进程时,我似乎无法进入断点集。最终我想使用 HTTPS。
我在 WCF 服务项目中的 web.config 文件:
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
客户端中的 App.config:
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="webBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IHttpHandler" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost/HttpMessageProcessor/HttpHandler.svc"
behaviorConfiguration="webBehavior" binding="webHttpBinding"
contract="Proxy.IHttpHandler" name="BasicHttpBinding_IHttpHandler" />
</client>
</system.serviceModel>
客户端代码:
using (var service = new Proxy.HttpHandlerClient())
{
}
Console.ReadKey();
我尝试了两件事来查看是否可以进入代码: 1. 点击CTRL+F5控制台项目以启动客户端,然后查看是否可以附加到 W3WP 进程。2.在调试模式下运行客户端,看看我是否可以指向一个URL,例如:
http://localhost/HttpMessageProcessor/HttpHandler.svc/HandleEchoRequest/
以上方法似乎都不起作用。
我已经在网上浏览了一些建议并尝试了一些东西,并且我依赖于我在这里提到的代码。欢迎任何建议/想法!
编辑 我遇到了这个网站并实施了一个简单的解决方案,如下所示。当我在浏览器中输入以下 URL 时,我可以成功命中 HandleMessage 实现中设置的断点:
http://localhost/TestWCFSite/TestWCFService.svc/HandleMessage
代码:
[ServiceContract]
public interface IService1
{
[OperationContract(Action = "*", ReplyAction = "*")]
void HandleMessage(Message m);
}
public class Service1 : IService1
{
public void HandleMessage(Message m)
{
//do something here.
}
}
服务的 App.config:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="False"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
我的客户代码:
Uri baseAddress = new Uri("http://localhost/TestWCFSite/TestWCFService.svc/");
Uri endpointAddress = new Uri("http://localhost/TestWCFSite/TestWCFService.svc/HandleMessage/");
var binding = new CustomBinding(new TextMessageEncodingBindingElement(MessageVersion.None, Encoding.UTF8), new HttpTransportBindingElement());
ServiceHost service = new ServiceHost(typeof(Service1), baseAddress);
service.AddServiceEndpoint(typeof(IService1), binding, endpointAddress);
service.Open();
var message = string.Format("Service open at {0} - press a key to continue", baseAddress);
Console.WriteLine(message);
Console.ReadKey();
service.Close();
我现在的问题是尝试在 IIS 中托管它并让它处理断点命中。