用于处理HTTP GET请求的WCF服务

时间:2013-01-03 22:44:00

标签: c# wcf .net-4.0

我需要创建一个位于服务器(托管在IIS中)的WCF服务,并侦听在特定URL上访问服务器的任何HTTP GET请求。

更具体地说,当有效的GET请求命中服务器时,Id喜欢检查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/

上述方法似乎都不起作用。

我已经看过网络寻求建议并尝试了一些事情,我已经在我提到的代码上休息了。欢迎任何建议/想法!

修改 我遇到了this网站并实施了一个简单的解决方案,如下所示。 当我在浏览器中输入以下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中托管它并让它处理断点命中。

1 个答案:

答案 0 :(得分:0)

假设您正在使用Visual Studio - 如果您通过工具附加会发生什么 - &gt;附加到流程...?

只是仔细检查一下,我假设你是从WCF服务项目而不是客户端项目中附加的? (我为这个答案道歉,我的代表太低,无法发表评论。)