自托管消费wcf服务

namespace WcfServiceLibrary1
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);
    }

    [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; }
        }
    }
}
namespace WcfServiceLibrary1
{
    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;
        }
    }
}
  <system.serviceModel>
    <services>
      <service name="WcfServiceLibrary1.Service1">
        <host>
          <baseAddresses>
            <add baseAddress = "http://localhost:8733/Design_Time_Addresses/WcfServiceLibrary1/Service1/" />
          </baseAddresses>
        </host>
        <endpoint address="" binding="basicHttpBinding" contract="WcfServiceLibrary1.IService1">
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

在wcf服务的程序集内定义一个代理类

namespace WcfServiceLibrary1
{
    public class MyCalculatorServiceProxy : ClientBase<IService1>, IService1
    {
        public string GetData(int value)
        {
            return base.Channel.GetData(value);
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            return base.Channel.GetDataUsingDataContract(composite);
        }
    }
}

客户端:

    class Program
    {
        static void Main(string[] args)
        {
            MyCalculatorServiceProxy proxy = new MyCalculatorServiceProxy();
            Console.WriteLine(proxy.GetData(5));
            Console.WriteLine(proxy.GetDataUsingDataContract(new CompositeType { BoolValue = false, StringValue = "xxx" }).StringValue);
            Console.Read();
        }
    }
  <system.serviceModel>
    <client>
      <endpoint address="http://localhost:8733/Design_Time_Addresses/WcfServiceLibrary1/Service1/" binding="basicHttpBinding" contract="WcfServiceLibrary1.IService1">
      </endpoint>
    </client>
  </system.serviceModel>

 

posted @ 2015-06-23 11:04  江境纣州  阅读(129)  评论(0编辑  收藏  举报