2014-07-02 4 views
1

Veuillez m'accompagner car je suis noob dans les services WCF/services Windows. J'ai créé un service WCF hébergé dans un service Windows. Je veux consommer le service WCF dans une application Silverlight dans le navigateur. Ci-dessous le fragment de code dans Silverlight pour accéder au service WCF:Le client Silverlight consommant le service WCF déclenche System.ServiceModel.CommunicationException


  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; 
     } 
    } 

je pourrais consommer le service WCF sans aucune exception dans une autre application non silverlight locale. Mais dans Silverlight, il jette l'exception System.ServiceModel.CommunicationException avec le message suivant:

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. 

Le innerException de type System.Net.Sockets.SocketException dit

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

googler autour suggéré moi des solutions comme la désactivation du pare-feu, l'ajout de balises suivantes dans ClientAccessPolicy.xml:

<grant-to> 
    <socket-resource port="4502-4534" protocol="tcp" /> 
    </grant-to> 

Malheureusement aucune des solutions ne m'a aidé. Je suppose que ce problème est lié à ClientAccessPolicy.xml. Je soupçonne qu'il me manque pour mettre ClientAccessPolicy.xml au bon endroit. Je l'ai gardé à la racine du service WCF. Cela fonctionnera-t-il pour netTcpBinding? Si ClientAccessPolicy.xml n'est pas le coupable, quelle peut être la cause de cette exception?

Répondre

0

Débarrassez-vous de l'exception .. !! J'ai ajouté attribut suivant avant la mise en œuvre du service:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 

Aslo J'ai fait enlever le mode de sécurité tout en spécifiant la liaison comme ceci:

var netTcpBinding = new NetTcpBinding(SecurityMode.None); 
Questions connexes