簡體   English   中英

使用Silverlight客戶端的WCF服務

[英]Silverlight client consuming WCF service

請耐心等待,因為我是WCF服務/ Windows服務的新手。 我創建了Windows服務中托管的WCF服務。 我想通過TCP在Silverlight瀏覽器中的應用程序中使用該WCF服務。 下面是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;
        }
    }

該代碼有時可以正常工作,但由於某些原因,它通常會引發臭名昭著的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.

類型System.Net.Sockets.SocketException的innerException說

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

此異常背后的可能原因是什么? 根據到目前為止的調查,我只能找到一個原因: ClientAccessPolicy.xml不正確。 可能還有其他原因嗎? 如果您有任何有用的資源,請提供相同的信息。 還有一個問題,如果我想讓Windows服務托管的WCF服務被LAN上的其他計算機占用,我必須進行哪些設置? 例如防火牆設置? 我的代碼無法訪問其他計算機上的WCF服務。 它引發了我上面提到的相同異常。 關於如何擺脫此異常的任何想法?

問題排序.. !! 我必須做以下事情:

1)在Windows服務中創建NetTcpBinding指定了SecurityMode.None

2)在具有高級安全性的Windows防火牆中創建了入站規則 ,以允許在端點地址中指定的端口上進行TCP通信。

暫無
暫無

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

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