110,534
社区成员
发帖
与我相关
我的任务
分享
[ServiceContract(CallbackContract = typeof(ISomeCallbackContract))]
public interface IService1
{
[OperationContract(IsOneWay = true)]
void DoSomething();
}
//用于回调的契约
public interface ISomeCallbackContract
{
[OperationContract(IsOneWay = true)]
void SomeCallbackMethod();
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Reentrant)]
public class Service1 : IService1
{
static List<ISomeCallbackContract> m_Callbacks = new List<ISomeCallbackContract>();
public void DoSomething()
{
ISomeCallbackContract callback = OperationContext.Current.GetCallbackChannel<ISomeCallbackContract>();
if (m_Callbacks.Contains(callback) == false)
{
m_Callbacks.Add(callback);
}
Console.WriteLine("这句话是在服务端执行的");
Thread.Sleep(5000);
callback.SomeCallbackMethod();
}
}
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="WcfService1.Service1" behaviorConfiguration="metadataExchange">
<host>
<baseAddresses>
<add baseAddress="http://footdream.gotoip3.com"/>
</baseAddresses>
</host>
<endpoint address="Service1.svc" binding="wsDualHttpBinding" bindingConfiguration="NoneSecurity" contract="WcfService1.IService1"/>
</service>
</services>
<bindings>
<wsDualHttpBinding>
<binding name="NoneSecurity" maxBufferPoolSize="12000000" maxReceivedMessageSize="12000000" useDefaultWebProxy="false">
<readerQuotas maxStringContentLength="12000000" maxArrayLength="12000000"/>
<security mode="None"/>
</binding>
</wsDualHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="metadataExchange">
<!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false -->
<serviceMetadata httpGetEnabled="true"/>
<!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
若要在调试过程中浏览 Web 应用程序根目录,请将下面的值设置为 True。
在部署之前将该值设置为 False 可避免泄露 Web 应用程序文件夹信息。
-->
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
public class CallBack : IService1Callback
{
public void SomeCallbackMethod()
{
Console.WriteLine("kjkj");
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
CallBack callback = new CallBack();
InstanceContext context = new InstanceContext(callback);
DuplexChannelFactory<IService1> channelFactory = new DuplexChannelFactory<IService1>(context, "WSDualHttpBinding_IService1");
IService1 service = channelFactory.CreateChannel();
service.DoSomething();
Console.Read();
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<wsDualHttpBinding>
<binding name="WSDualHttpBinding_IService1">
<security mode="None" />
</binding>
</wsDualHttpBinding>
</bindings>
<client>
<endpoint address="http://footdream.gotoip3.com/Service1.svc"
binding="wsDualHttpBinding" bindingConfiguration="WSDualHttpBinding_IService1"
contract="aaa.IService1" name="WSDualHttpBinding_IService1" />
</client>
</system.serviceModel>
</configuration>