簡體   English   中英

具有異步模式的WCF ChannelFactory

[英]WCF ChannelFactory with Async pattern

我有以下服務合同

[ServiceContract]
public interface IDataService
{
    [OperationContract]
    void UpdateProcessingState(ActionState state);
}

但是我想允許客戶端ChannelFactory訪問異步模式,所以我繼承了上述合同

[ServiceContract]
public interface IDataServiceClient : IDataService
{
    [OperationContract(AsyncPattern = true, IsOneWay = true)]
    IAsyncResult BeginUpdateProcessingState(ActionState state, AsyncCallback asyncCallback, object obj = null);

    void EndUpdateProcessingState(IAsyncResult r);

}

我希望這將允許我在服務器上使用以下內容,而不必實現異步存根,即

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Multiple)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class DataService : IDataService
{
    public void UpdateProcessingState(ActionState state)
    {
    }

    //public IAsyncResult BeginUpdateProcessingState(ActionState state, AsyncCallback asyncCallback, object obj = null)
    //{
    //    throw new NotImplementedException();
    //}

    //public void EndUpdateProcessingState(IAsyncResult r)
    //{
    //    throw new NotImplementedException();
    //}
}

給客戶端ChannelFactory訪問異步方法的同時

ChannelFactory = new ChannelFactory<IDataServiceClient>(GetBinding(), GetEndpoint());
ChannelFactory.Endpoint.Behaviors.Add(new ProtoEndpointBehavior());
_proxy = ChannelFactory.CreateChannel();

Proxy.BeginUpdateProcessingState(state, ar =>
{

});

但是客戶端拋出以下錯誤,我想我能理解為什么,我不確定如何解決它,或者即使我嘗試做的事情是可能的,我也強調了我認為與之相關的部分問題

“ IDataService”類型的同步OperationContract方法“ UpdateProcessingState”與異步“ OperationContract”方法“ BeginUpdateProcessingState”和“ EndUpdateProcessingState”相匹配,因為它們具有相同的操作名稱“ UpdateProcessingState”。 當一個同步OperationContract方法與一對異步OperationContract方法匹配時,兩個OperationContracts的“操作”屬性必須具有相同的值。 在這種情況下,值是不同的 要修復此問題,請更改其中一個OperationContracts的'Action屬性以使其與另一個匹配。 或者,更改其中一種方法的名稱將阻止匹配。

注意 :

我知道我可以將存根添加到服務中,並且一切都很好,但是能夠將合同分為服務和客戶(或者更具體地說,是從服務合同繼承的客戶合同)會很好

這可能嗎?

好像我在基本合同中放了一個名稱空間和一個名字

[ServiceContract(Namespace = "http://schemas.jcurve.com/", Name = "DataService")]
public interface IDataService
{
}

並在繼承合同中引用它,似乎一切正常

[OperationContract(AsyncPattern = true, 
    Action = "http://schemas.jcurve.com/DataService/UpdateProcessingState", 
    ReplyAction = "http://schemas.jcurve.com/DataService/UpdateProcessingStateResponse")]
IAsyncResult BeginUpdateProcessingState(ActionState state, AsyncCallback asyncCallback, object obj = null);

注意:也許有人對此有更多的意見,但是我需要在合同中添加一個名稱空間和名稱,並在操作中正確設置網址格式

即在這種情況下采取行動

http://<NameSpace>/<Name>/<Method>

並回復動作

http://<NameSpace>/<Name>/<Method>Repsonse

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM