zoukankan      html  css  js  c++  java
  • WCF的几种寄宿方式

    1、 WCF服务的控制台程序寄宿

    namespace WcfService_HostConsole
    {
        class Program
        {
            static void Main(string[] args)
            {            
                try
                {
                    ServiceHost serviceHost = new ServiceHost(typeof(Service1));
                    if (serviceHost.State != CommunicationState.Opened)
                    {
                        serviceHost.Open();
                    }
    
                    Console.WriteLine("WCF 服务正在运行......");
                    Console.WriteLine("输入回车键 <ENTER> 退出WCF服务");
                    Console.ReadLine();
                    serviceHost.Close();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
            }
        }
    }

    2、WCF服务的Winform程序寄宿

    namespace WcfService_HostWinform
    {
        public partial class FrmMain : Form
        {
            ServiceHost serviceHost = null;
            BackgroundWorker worker = null;
    
            public FrmMain()
            {
                InitializeComponent();
    
                worker = new BackgroundWorker();
                worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
                worker.DoWork += new DoWorkEventHandler(worker_DoWork);
    
                if (!worker.IsBusy)
                {
                    textBox1.Text = "正在启动......";
                    worker.RunWorkerAsync();
                }
            }
    
            void worker_DoWork(object sender, DoWorkEventArgs e)
            {
                try
                {
                    serviceHost = new ServiceHost(typeof(Service1));
                    if (serviceHost.State != CommunicationState.Opened)
                    {
                        serviceHost.Open();
                    }
    
                    e.Result = "正常";
                }
                catch (Exception ex)
                {
                    e.Result = ex.Message;
                }
            }
    
            void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
                if (e.Result != null)
                {
                    if (e.Result.ToString() == "正常")
                    {
                        textBox1.Text = "服务正在进行侦听......";
                    }
                    else
                    {
                        textBox1.Text = string.Format("错误:{0}", e.Result);
                    }
                }
            }
        }
    }

    3、 使WCF服务支持GET方式调用

    namespace WcfServiceForWinform
    {
        [ServiceContract]
        public interface IService1
        {
            [OperationContract]
            void DoWork();
    
            [OperationContract]
            [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)]
            string GetData(int value);
        }
    }
    namespace WcfServiceForWinform
    {
        [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
        public class Service1 : IService1
        {
            public void DoWork()
            {
            }
    
            public string GetData(int value)
            {
                return string.Format("You entered: {0}", value);
            }
        }
    }
    <?xml version="1.0"?>
    <configuration>
    
      <system.web>
        <compilation debug="true"/>
      </system.web>
    
      <system.serviceModel>
        <services>
          <service name="WcfServiceForWinform.Service1"  behaviorConfiguration="ServiceConfig">
            <endpoint address="" binding="webHttpBinding"
                      bindingConfiguration="webHttpBindingWithJsonP" behaviorConfiguration="webHttpBehavior"
                      contract="WcfServiceForWinform.IService1">
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:9000/Service1/" />
              </baseAddresses>
            </host>
          </service>
        </services>
    
        <bindings>
          <webHttpBinding>
            <binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true"  />
          </webHttpBinding>
        </bindings>
        
        <behaviors>
          <endpointBehaviors>
            <behavior name="webHttpBehavior">
              <webHttp/>
            </behavior>
          </endpointBehaviors>
          
          <serviceBehaviors>
            <behavior name="ServiceConfig">
              <serviceMetadata httpGetEnabled="True" policyVersion="Policy15"/>
              <serviceDebug includeExceptionDetailInFaults="False"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
    
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
      </system.serviceModel>
    
      <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
      </startup>
    </configuration>
  • 相关阅读:
    021.day21 反射 Class类 反射常用操作
    020.day20 线程概述 多线程优缺点 线程的创建 线程常用方法 生命周期 多线程同步
    019.day19 缓冲流 对象流 标准输入输出流
    018.day18 map集合如何实现排序 File类 IO流 字节流 字符流 编码
    017.day17 Map接口 克隆 treeSet集合排重缺陷
    016.day16 HashSet TreeSet 比较器Comparable Comparator
    015.day15
    014.day14
    013.day13
    线程
  • 原文地址:https://www.cnblogs.com/xinzheng/p/5678911.html
Copyright © 2011-2022 走看看