简体   繁体   English

使用Silverlight客户端的WCF服务

[英]Silverlight client consuming WCF service

Please bear with me as I am new to WCF services/ Windows services. 请耐心等待,因为我是WCF服务/ Windows服务的新手。 I've created a WCF service hosted in a Windows service. 我创建了Windows服务中托管的WCF服务。 I want to consume that WCF service in a Silverlight in-browser application over TCP. 我想通过TCP在Silverlight浏览器中的应用程序中使用该WCF服务。 Below is the code fragment in Silverlight to access WCF service: 下面是Silverlight中访问WCF服务的代码片段:

        var messageEncoding = new BinaryMessageEncodingBindingElement();
        var tcpTransport = new TcpTransportBindingElement();
        var binding = new CustomBinding(messageEncoding, tcpTransport);

        // Create a channel factory for the service endpoint configured with the custom binding.
        var cf = new ChannelFactory<ICalcService>(binding, new EndpointAddress("net.tcp://192.168.2.104:4508"));

        // Open the channel.
        ICalcService channel = cf.CreateChannel();

        // Invoke the method asynchronously.
        channel.BeginAdd(9, 5, AddCallback, channel);

    private void AddCallback(IAsyncResult asyncResult)
    {
        try
        {
            double endAdd = ((ICalcService) asyncResult.AsyncState).EndAdd(asyncResult);
        }
        catch (Exception exception)
        {
            throw exception;
        }
    }

The code works fine sometimes but often it throws an infamous System.ServiceModel.CommunicationException with the following message for some reasons: 该代码有时可以正常工作,但由于某些原因,它通常会引发臭名昭著的System.ServiceModel.CommunicationException并显示以下消息:

Could not connect to net.tcp://192.168.2.104:4508/. The connection attempt lasted for a time span of 00:00:00.1010058. TCP error code 10013: An attempt was made to access a socket in a way forbidden by its access permissions.. This could be due to attempting to access a service in a cross-domain way while the service is not configured for cross-domain access. You may need to contact the owner of the service to expose a sockets cross-domain policy over HTTP and host the service in the allowed sockets port range 4502-4534.

The innerException of type System.Net.Sockets.SocketException says 类型System.Net.Sockets.SocketException的innerException说

An attempt was made to access a socket in a way forbidden by its access permissions.

What are the possible reasons behind this exceptions? 此异常背后的可能原因是什么? Based on what I investigated so far, I could find only one reason: Improper ClientAccessPolicy.xml . 根据到目前为止的调查,我只能找到一个原因: ClientAccessPolicy.xml不正确。 What may be the other reasons? 可能还有其他原因吗? If you have any useful resources, please provide me the same. 如果您有任何有用的资源,请提供相同的信息。 One more question, if I want to make Windows service hosted WCF service to get consumed by other machines on LAN, what settings do I have to make? 还有一个问题,如果我想让Windows服务托管的WCF服务被LAN上的其他计算机占用,我必须进行哪些设置? Eg firewall settings? 例如防火墙设置? My code cannot access WCF service on other machine. 我的代码无法访问其他计算机上的WCF服务。 It throws the same exception I mentioned above. 它引发了我上面提到的相同异常。 Any ideas about how to get rid of this exception? 关于如何摆脱此异常的任何想法?

Problem sorted..!! 问题排序.. !! I had to do following things: 我必须做以下事情:

1) Specified SecurityMode.None while creating NetTcpBinding in Windows service. 1)在Windows服务中创建NetTcpBinding指定了SecurityMode.None

2) Created an Inbound Rule in Windows Firewall With Advanced Security to allow TCP traffic on the port I specified in the end point address. 2)在具有高级安全性的Windows防火墙中创建了入站规则 ,以允许在端点地址中指定的端口上进行TCP通信。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM