0
我有一個WCF服務,其中一切都以編程方式啓動(並需要繼續這樣做),我希望此服務響應[WebGet]
屬性。與WebGet的WCF服務不起作用
但是,當我調用其中一個WCF方法時,該服務將返回400錯誤請求。 這最初看起來像是WCF Service Returns 400 error when using WebGet或Bad Request 400 while accessing WCF Rest service (WebGet)的副本,但這兩個解決方案都添加到web.config
,這是我沒有的文件,因爲我需要以編程方式執行所有操作。
我試圖添加一個WebHttpBinding,看起來像端點,但它不能正常工作(我可能不是正確的方式)。下面
的代碼開始沒有錯誤,但是當我嘗試去http://localhost:8765/MyService/MyTest
我得到上述400 Bad Request
我缺少什麼?
WCF啓動
MyService myService = new MyService();
ServiceHost host = new ServiceHost(myService, new Uri("http://localhost:8765/MyService"));
ServiceBehaviorAttribute behavior = host.Description.Behaviors.Find<ServiceBehaviorAttribute>();
behavior.InstanceContextMode = InstanceContextMode.Single;
foreach(ServiceEndpoint endpoint in host.Description.Endpoints) {
endpoint.Binding = new WebHttpBinding();
}
host.Open();
服務接口
[ServiceContract]
public interface IMyService {
[OperationContract]
[WebGet]
string MyTest();
}
服務實現
public class MyService : IMyService {
public string MyTest() {
return "Response from WCF service";
}
}
我認爲關鍵要我錯過了什麼是'WebServiceHost'並添加一個新的'WebHttpBinding'作爲一個新的端點(帶有空白地址)發送給主機。做這兩件事情使它工作。 – GTHvidsten