动态调用WCF

第一部分:服务端

using System;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Windows.Forms;

namespace WCFService
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string url = "http://localhost:31125/TestWCF";
                Uri baseAddress = new Uri(url);
                ServiceHost _host = new ServiceHost(typeof(TestWCF), baseAddress);
                _host.AddServiceEndpoint(typeof(ITestWCF), new WSHttpBinding(SecurityMode.None), url);
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true;
                _host.Description.Behaviors.Add(smb);
                _host.Open();
            }
            catch (Exception ex)
            {
            }
            button1.Text = "服务启动成功";
            button1.Enabled = false;
        }
    }


    public class TestWCF : ITestWCF
    {
        #region ITWCF 成员

        public string GetTest(string studyUid)
        {
            return "这是WCF方法一调用:" + studyUid;
        }

        #endregion

        #region ITWCF 成员


        public string OtherTestMethod(string studyUid)
        {
            return "这是WCF方法二调用:" + studyUid;
        }

        #endregion
    }

    [ServiceContract()]
    public interface ITestWCF
    {
        [OperationContract]
        string GetTest(string studyUid);

        [OperationContract]
        string OtherTestMethod(string studyUid);
    }
}


第二部分:客户端相关类

#region 调用WCF相关类

    [System.ServiceModel.ServiceContractAttribute(ConfigurationName = "ITestWCF")]
    public interface ITestWCF
    {

        [System.ServiceModel.OperationContractAttribute()]
        string GetTest(string studyUid);

        [System.ServiceModel.OperationContractAttribute()]
        string OtherTestMethod(string studyUid);
    }

    public interface MiddleChannel : ITestWCF, System.ServiceModel.IClientChannel
    {
    }

    [System.Diagnostics.DebuggerStepThroughAttribute()]
    public partial class TestWCFClient : System.ServiceModel.ClientBase<ITestWCF>, ITestWCF
    {

        public TestWCFClient()
        {
        }
        public TestWCFClient(System.ServiceModel.Channels.Binding binding, EndpointAddress remoteAddress)
            : base(binding, remoteAddress)
        {

        }

        public string GetTest(string studyUid)
        {
            return base.Channel.GetTest(studyUid);
        }

        public string OtherTestMethod(string studyUid)
        {
            return base.Channel.OtherTestMethod(studyUid);
        }
    }
    #endregion

 


第三部分:客户端调用

private void btnWCF_Click(object sender, EventArgs e)
        {             
            WSHttpBinding binding = new WSHttpBinding(SecurityMode.None);
            EndpointAddress remoteAddress = new EndpointAddress("http://localhost:31125/TestWCF");
            TestWCFClient tc = new TestWCFClient(binding, remoteAddress);
            string kkk=tc.GetTest("");
        }

posted @ 2014-11-27 17:02  星释天狼  阅读(156)  评论(0)    收藏  举报