2011-05-04 28 views
2

我目前使用Silverlight版本的svcutil来为Silverlight生成.cs文件。我希望能够与.NET 3.5分享,但似乎存在一些障碍。值得注意的是,ChannelBase似乎并不存在于.NET中,IHttpCookieContainerManager也不存在。是否有可能医生我的Service.cs,以便它们都可读? (我不喜欢使用.config文件。)如何为Silverlight和.NET生成WCF .cs文件?

+0

为什么不在Visual Studio中为每个解决方案添加一个服务引用,并让VS为每个解决方案创建.cs文件。 – 2011-05-04 19:09:03

回答

1

如果你不使用svcutil,你可以轻松地做到这一点。如果您的服务接口由Silverlight和.Net 3.5共享,则只需使用一些simple code即可在运行时创建客户端。

注意:由于Silverlight只支持异步通信,因此您需要创建两个略有不同的接口。或者,您可以使用相同的接口并使用#if SILVERLIGHT来告知编译器在编译Silverlight代码时仅编译文件的一部分,并在编译.NET代码时编译该文件的另一部分。举个例子:

[ServiceContract(Namespace="http://www.example.com/main/2010/12/21")] 
public interface IService 
{ 
#if SILVERLIGHT 
     [OperationContract(AsyncPattern=true, Action = "http://www.example.com/HelloWorld", ReplyAction = "http://www.example.com/HelloWorldReply")] 
     IAsyncResult BeginHelloWorld(AsyncCallback callback, object state); 
     string EndHelloWorld(IAsyncResult result); 
#else 
     [OperationContract(Action="http://www.example.com/HelloWorld", ReplyAction="http://www.example.com/HelloWorldReply")] 
     string HelloWorld(); 
#endif 
} 

这允许您使用Silverlight时,或者只是myClient.HelloWorld()使用.net 3.5时,将调用myClient.BeginHelloWorld()和myClient.EndHelloWorld()。

如果您有很多自定义绑定正在进行,您还可以创建一个继承自CustomBinding的类,并让该类在.NET和Silverlight之间共享。这样的类的一个例子:

public class MyServiceBinding : CustomBinding 
{ 
    public MyServiceBinding() 
    { 
     BinaryMessageEncodingBindingElement binaryEncodingElement = new BinaryMessageEncodingBindingElement(); 

#if !SILVERLIGHT 
     binaryEncodingElement.ReaderQuotas.MaxArrayLength = int.MaxValue; 
#endif 

     Elements.Add(binaryEncodingElement); 
     Elements.Add(new HttpTransportBindingElement() { MaxReceivedMessageSize = int.MaxValue }); 
    } 
} 
相关问题