我對從asp.net應用程序調用WCF服務存在很大疑問。我實現了雙通道並回撥合約。我使用的是conurrency模式是可重入的,而實例模式是每個會話。我可以打電話給wcf服務,它會觸發回撥方法。直到這一點,一切都很好。舉辦第一次服務電話,並在ASP中使用WCF進行第二次呼叫
但我想到了一種方式,當我第一次打電話給服務時,我使用waithandler對象在該服務方法中保持該電話並觸發了回撥方法。這個回調命中客戶端代碼,並且再次從客戶端調用同一服務類中的另一個方法來釋放該等待處理程序鎖定。當我使用等待處理程序鎖定第一個呼叫並觸發回撥方法時,它將回撥給客戶端,然後再次調用同一服務中的另一個方法。但是,我得到超時的例外......保持第一個電話,並在同一個服務中打第二個電話是可能的?我是很新的ASP.Net
[ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(IMyServiceCallBacks))]
public interface IMyService
{
[OperationContract]
string GetData1();
[OperationContract]
void GetData2(bool isOK);
}
public interface IMyServiceCallBacks
{
[OperationContract(IsOneWay = true)]
void MyCallBack();
}
這是服務類
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Reentrant)]
public class MyService : IMyService
{
EventWaitHandle waitHandle;
bool isook = false;
public MyService()
{
}
string IMyService.GetData1()
{
waitHandle = new EventWaitHandle(false, EventResetMode.AutoReset);
CallMethod();
bool isOK = waitHandle.WaitOne();
return "success";
}
private void CallMethod()
{
OperationContext.Current.GetCallbackChannel<IMyServiceCallBacks>().MyCallBack();
}
public void GetData2(bool isOK)
{
isook = true;
waitHandle.Reset();
}
}
下面是客戶端代碼
[CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant, UseSynchronizationContext = false, IncludeExceptionDetailInFaults = true)]
public class MyProxyClient : MyService, MyServiceCallback
{
bool isOK;
private MyServiceClient caServiceProxy
private InstanceContext myInstanceContext;
public MyService()
{
myInstanceContext = new InstanceContext(this);
EndpointAddress endPointAddress = new EndpointAddress("http://localhost:2222222/MyWCF1/MyService.svc");
caServiceProxy = new MyServiceClient(myInstanceContext, new WSDualHttpBinding(), endPointAddress);
}
public string GetData1()
{
string hhhh = caServiceProxy.GetData1();
return hhhh;
}
public void GetData2(bool isOK)
{
caServiceProxy.GetData2(isOK); // here it is processing but finally time out
}
// This is the call back
public void MyCallBack()
{
isOK = true;
GetData2(isOK);
}
}
及以下web配置serviceModeltag
<system.serviceModel>
<services>
<service name="MyABService.MyService" behaviorConfiguration="WCFDuplex.Service1Behavior">
<!-- Service Endpoints -->
<endpoint address="" binding="wsDualHttpBinding" contract="MyABService.IMyService">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WCFDuplex.Service1Behavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
我用svcutil生成代理。 任何想法???
感謝
它有助於發佈您嘗試過的代碼(或其總和)。 – qw3n
上面添加了使用的代碼。有關它的任何想法? –