none
WCF REST 服务host到exe程序时,报没有找到服务异常 RRS feed

  • 问题

  • 首先问一下,WCF REST服务是否可以Host到Exe程序(不是host到IIS)?

    我通过下面的代码去实现,访问service时报Remote server NotFound 例外。

    WCFService代码

        [ServiceContract]
        public interface IService
        {
            [WebInvoke(UriTemplate = "/RequestResponse",
                        Method = "POST",
                        RequestFormat= WebMessageFormat.Xml,
                        ResponseFormat = WebMessageFormat.Xml,
                        BodyStyle = WebMessageBodyStyle.Bare)]
            [OperationContract]
            ResponseResult RequestResponse(Request request);
        }

    实现代码: 略

    Host程序代码(Windows程序)

       public partial class Form1 : Form
        {
    
            ServiceHost srviceHost = null;
            bool isServiceStarted = false;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                if (isServiceStarted == false)
                {
                    StartService();
                }
                else
                {
                    StopService();
                }
            }
    
            private void StartService()
            {
                try
                {
                    Uri serviceUri = new Uri("http://192.168.1.1:8399/WCFService");
    
                    smartDeviceServiceHost = new ServiceHost(typeof(Service), serviceUri);
                    serviceHost.Open();
    
                    button1.Text = "起動成功";
                    isServiceStarted = true;
                }
                catch
                {
                    button1.Text = "起動失敗";
                }
            }
    
            private void StopService()
            {
                try
                {
                    serviceHost.Close();
                    button1.Text = "終了成功";
                    isServiceStarted = false;
                }
                catch
                {
                    button1.Text = "終了失敗";
                }
            }
        }

    app.config

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <system.serviceModel>
            <behaviors>
              <serviceBehaviors>
                <behavior name="serviceBehavior">
                  <serviceMetadata httpGetEnabled="true" />
                </behavior>
              </serviceBehaviors>
    
              <endpointBehaviors>
                <behavior name="webHttp">
                  <webHttp helpEnabled="true" />
                </behavior>
              </endpointBehaviors>        
            </behaviors>
            <services>
                <service behaviorConfiguration="serviceBehavior" name="Service">
                    <endpoint address="http://192.168.1.1:8399/WCFService"
                              binding="webHttpBinding" 
                              behaviorConfiguration="webHttp"
                              contract="IService" />
                </service>          
             </services>
      </system.serviceModel>
    
      <system.web>
        <compilation debug="true" />
      </system.web>
    </configuration>

    客户端

     private void SendRequest(Request rq)
          {
                XmlSerializer xmlSeria = new XmlSerializer(typeof(Request));
                MemoryStream ms = new MemoryStream();
                xmlSeria.Serialize(ms, rq);
                m_buf = ms.GetBuffer();
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://192.168.1.1:8399/WCFService/RequestResponse");
                
                myRequest.Method = "POST";
                myRequest.ContentLength = m_buf.Length;
                myRequest.ContentType = "text/html";
                myRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), myRequest);
            }
    
    
            private void GetRequestStreamCallback(IAsyncResult ar)
            {
                WebRequest req = ar.AsyncState as WebRequest;
                using (Stream writer = req.EndGetRequestStream(ar))
                {
                    writer.Write(m_buf, 0, m_buf.Length);
                    writer.Flush();
                    writer.Close();
                }
    
                req.BeginGetResponse(new AsyncCallback(PostResponseCallback), req);
            }
    
            private void PostResponseCallback(IAsyncResult ar)
            {
                WebRequest req = ar.AsyncState as WebRequest;
                HttpWebResponse res = (HttpWebResponse)req.EndGetResponse(ar);
    
                Stream stream = res.GetResponseStream();
    
                XDocument xdoc = XDocument.Load(stream);
                ResponseDataProc(xdoc);
            }

    2012年7月6日 12:44

答案

全部回复