我需要創建一個位於服務器(託管在IIS中)的WCF服務,並偵聽任何HTTP GET請求在某個URL處擊中服務器。用於處理HTTP GET請求的WCF服務
更具體地說,當一個有效的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流程時,我似乎無法進入轉折點集。最終Id喜歡使用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網站,並實現了一個簡單的解決方案如下。 我能成功擊中斷點的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中承載它並讓它處理斷點命中。
嘿,你好 - 使用Visual Studio。我在同一個解決方案中獲得了客戶端和WCF服務。當我附上時,它表示客戶端代碼不會被納入..合理。 WCF代碼是鮮紅的,這意味着它應該打... – TheRenoRanger