VS2010 创建WCF以及SL的客户端如何调用WCF服务教程(一): 创建WCF

经过一整天的自我摸索, 终于搞定SL如何调用WCF, ⊙�n⊙b汗.

谨以此文纪念.

  1. 1. 打开VS2010, 点击"File/New/Project"
  2. 2. 点击左边"Other Languages/Visual C#", 选择右边的"WCF Service Application", 如图:

3. 设置好工程名字和路径后, 点击"Ok”后, vs2010默认生成了一个简单的Service, 代码如下:

定义服务契约

    Code    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.        [ServiceContract]      public   interface   IService1      {            [OperationContract]          string   GetData(int   value  );            [OperationContract]          CompositeType GetDataUsingDataContract(CompositeType composite);            // TODO: Add your service operations here        }          // Use a data contract as illustrated in the sample below to add composite types to service operations.        [DataContract]      public   class   CompositeType      {          bool   boolValue = true  ;          string   stringValue = "Hello   ";            [DataMember]          public   bool   BoolValue          {              get   { return   boolValue; }              set   { boolValue = value  ; }          }            [DataMember]          public   string   StringValue          {              get   { return   stringValue; }              set   { stringValue = value  ; }          }      }    

实现服务契约
    Code   // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.        public   class   Service1 : IService1      {          public   string   GetData(int   value  )          {              return   string  .Format("You entered: {0}  ", value  );          }            public   CompositeType GetDataUsingDataContract(CompositeType composite)          {              if   (composite == null  )              {                  throw   new   ArgumentNullException("composite  ");              }              if   (composite.BoolValue)              {                  composite.StringValue += "Suffix  ";              }              return   composite;          }      }    

4. F7编译后, F5运行, 效果如下:

(P.S. Windows Communication Foundation (WCF) 测试客户端 (WcfTestClient.exe) 是一个 GUI 工具,使用该工具,用户可以输入测试参数、将该输入提交给服务并查看服务发回的响应。当与 WCF 服务主机结合时,它可以提供完美的服务测试体验。)

5. 经过前面的几个步骤, 我们已经建立了一个可以供Silverlight测试使用的Service.

下一节, 我将给大家讲解如何在SL中调用此Service.

你可能感兴趣的:(String,service,silverlight,interface,WCF,2010)