WCF 中有沒有辦法根據它收到的請求型別檢查一些邏輯?這可以在實際的服務端點代碼中完成嗎?
例如:
服務初始化后,我的服務收到一個 PUT 請求。在 myService.svc.cs 我希望有如下邏輯:
if httpRequest.Type == PUT
{
//Do Something
}
這可能嗎?我確信有比為每個 PUT 型別的操作合同添加邏輯更好的方法來處理請求。抱歉,如果這個問題沒有意義,我對 WCF 有點陌生,并且正在努力學習。如果您需要澄清劑,請告訴我。
編輯:
這是 myService.svc.cs 當前的樣子:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public partial class MyService: IMyService
{
public async Task<someObject> GetMethod1 () // Some GET Method
{
doSomethingForGetRequests();
//method implementation
}
public async Task<someObject> GetMethod2 () // Some GET Method
{
doSomethingForGetRequests();
//method implementation
}
public async Task<someObject> PutMethod1 () // Some PUTMethod
{
doSomethingForPutRequests();
//method implementation
}
doSomethingForPutRequests()
{
if(config.IsReadOnly)
{
throw new WebFaultException(HttpStatusCode.BadRequest);
}
}
}
I am wondering if there is a place where i can place doSomethingForGetRequest() and doSomethingForPutRequest() in a central location before the request reaches these methods so I don't have to add these methods to each one of my Service Methods.
Would global.asax.cs Application_BeginRequest() be an appropriate place for this logic?
uj5u.com熱心網友回復:
也許訊息檢查器可以幫助您,它會在到達服務的每個請求上呼叫。
IDispatchMessageInspector定義了在服務應用程式中啟用自定義檢查或修改入站和出站應用程式訊息的方法。
您可以查看這些帖子:
檢測操作是 POST 還是 GET 方法
為 WCF REST 中的每個請求自動呼叫該方法
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/424803.html
上一篇:通過不同埠上的HAPRoxy服務器轉發WCF流量時出現AddressFilter不匹配
下一篇:在WCF中使用多個命名空間
