


一:新建wcf服务 UserCallBack.svc
- [ServiceContract(CallbackContract = typeof(ICallback))] //指定UserCallBack回调接口
- public interface IUserCallBack
- {
- [OperationContract]
- ReturnData<string> Login(string username, string password);
- }
-
-
- public interface ICallback //回调接口客服端实现
- {
-
- [OperationContract(IsOneWay = true)]//单向调用,不需要返回值
- void LoginCallBack(string hello);
-
- [OperationContract(IsOneWay = true)]//单向调用,不需要返回值
- void Test(string hello);
-
- }
-
-
-
- //实现IUserCallBack接口
-
- public ReturnData<string> Login(string username, string password)
- {
- DaoCommon.sesskv.Add(_sessionId, OperationContext.Current.GetCallbackChannel<ICallback>());//记录通道
-
- ICallback callback = DaoCommon.sesskv[_sessionid];
- Thread oThread = new Thread(delegate()
- {
- try
- { callback.LoginCallBack("successful"); } //回调客服端方法
- catch { }
- });
- oThread.Start();
-
- OperationContext.Current.Channel.Closing += Channel_Closing;
- }
-
-
-
- void Channel_Closing(object sender, EventArgs e)
- {
- Logout(sessionGlo);
- }
二:配置
- <!--这里是添加的开始-->
- <services>
-
- <service name="JSDService.Users.UserCallBack" >
- <endpoint address="" binding="netTcpBinding" bindingConfiguration="netTcpBindConfig" contract="JSDService.Users.IUserCallBack"/>
- <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
- </service>
-
-
- </services>
- <extensions>
- <bindingExtensions>
- <add name="pollingDuplexHttpBinding" type="System.ServiceModel.Configuration.PollingDuplexHttpBindingCollectionElement,System.ServiceModel.PollingDuplex"/>
- </bindingExtensions>
- </extensions>
- <!--这里是添加的结束-->
-
- <bindings>
- <netTcpBinding>
-
- <!--超时时间-->
- <binding name="netTcpBindConfig" receiveTimeout="00:20:00">
- <security mode="None"/>
- </binding>
- </netTcpBinding>
- </bindings>
三:发布到IIS
错误:找不到具有绑定 NetTcpBinding 的终结点的与方案 net.tcp 匹配的基址。注册的基址方案是 [http]。
这是因为iis中没有配置对TCP的支持,安装iis支持tcp需要控制面板>程序和功能>打开和关闭windows功能,安装Non-Http支持
安装后,在启用WCF服务时出现错误“未能从程序集“System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”中加载 ”
修改配置文件 %windir%/system32/inetsrv/config/applicationHost.config中 runtimeVersionv2.0" />
在 Visual Studio 2010 命令提示符下运行下面的命令行: aspnet_regiis.exe -i -enable或者用系统命令行,打开位于下列地址的 %windir%/Microsoft.NET/Framework/v4.0.30319 中执行aspnet_regiis.exe -iru
打开IIS找到你的网站 绑定添加net.tcp 可以把绑定信息设置成4502:*
然后选择你的网站,点击“高级设置”,弹出的的窗体中,在“已启用的协议”一栏中手动添加:net.tcp
错误: 10061服务器拒绝连接
这是因为有几个服务没有开启
在控制面板中开打如下服务
Net.Tcp Listener Adapter 与Net.Tcp Port Sharing Service
TCP 错误代码 10013:
如果能在地址栏正确的访问了出现此问题就是没有加入跨域配置文件,需要注意的是比如我们的IIS目录指向chat.Web项目,浏览端口是809,我们的IIS里还有另一个网站使用80端口,这时,我们就要把跨域文件clientaccesspolicy.xml放到使用80端口的网站下,这是由于SilverLight会首先到80端口下检查跨域文件是否允许它访问。
clientaccesspolicy.xml
- <?xml version="1.0" encoding="utf-8"?>
- <access-policy>
- <cross-domain-access>
- <policy>
- <allow-from http-request-headers="*">
- <domain uri="*"/>
- </allow-from>
- <grant-to>
- <resource path="/" include-subpaths="true"/>
- <socket-resource port="4502-4530" protocol="tcp" />
- </grant-to>
- </policy>
- </cross-domain-access>
- </access-policy>
crossdomain.xml:
- <?xml version="1.0" ?>
- <cross-domain-policy>
- <allow-access-from domain="*" />
- </cross-domain-policy>
错误:套接字连接已中止。这可能是由于处理消息时出错或远程主机超过接收...
一般是使用netTcpBinding时报错的
修改下配置文件
把netTcpBinding改成mexTcpBinding就可以了
四:siverlight客服端
只需添加引用服务,注册好回调事件就行
- public MainPage()
- {
- InitializeComponent();
-
-
- aa.Click += aa_Click;
- mm = new UserCallBack.UserCallBackClient();
- mm.LoginCompleted += mm_LoginCompleted;
- mm.LoginCallBackReceived += mm_LoginCallBackReceived;
- mm.RunALotteryCqReceived += mm_RunALotteryCqReceived;
- }
-
-
- void mm_RunALotteryCqReceived(object sender, UserCallBack.RunALotteryCqReceivedEventArgs e)
- {
- MessageBox.Show("开奖了!!");
- }
-
-
- void mm_LoginCallBackReceived(object sender, UserCallBack.LoginCallBackReceivedEventArgs e)
- {
- MessageBox.Show("登陆回调了");
- }
-
-
- void mm_LoginCompleted(object sender, UserCallBack.LoginCompletedEventArgs e)
- {
- MessageBox.Show("登陆了"+e.Result.Error);
- }
-
- private void sdf_Click(object sender, RoutedEventArgs e)
- {
- mm.LoginAsync(tb.Text,tb2.Text);
- }
注意:在siverlight添加对net.ctp服务的引用时是用的计算机名+端口但是不在同个域内,需要dns,类似主机名这样才能找到对方
需要需要把计算机名改成ip,用wcftestclient测试他会报dns无法解析
注意:添加引用的时候要使用tcp那个端口,比如上面绑定的4205
例如:net.tcp://ip:4502/SendFileWcf.svc
欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739。有需要软件开发,或者学习软件技术的朋友可以和我联系~(Q:815170684)