2013-10-18 154 views
2

我在控制檯應用程序(.NET 4.0)中託管WCF服務。服務代碼(從msdn爲例):在控制檯/ WinForms中通過ServiceHost託管WCF服務

using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.ServiceModel.Web; 

namespace WCFServiceHost 
{ 
    [ServiceContract(Namespace = "WCFServiceHost")] 
    public interface ICalculator 
    { 
     [WebInvoke(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)] 
     MathResult DoMathJson(double n1, double n2); 

     [WebInvoke(ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped)] 
     MathResult DoMathXml(double n1, double n2); 

    } 

    public class CalculatorService : ICalculator 
    { 

     public MathResult DoMathJson(double n1, double n2) 
     { 
      return DoMath(n1, n2); 
     } 

     public MathResult DoMathXml(double n1, double n2) 
     { 
      return DoMath(n1, n2); 
     } 

     private MathResult DoMath(double n1, double n2) 
     { 
      MathResult mr = new MathResult(); 
      mr.sum = n1 + n2; 
      mr.difference = n1 - n2; 
      mr.product = n1 * n2; 
      mr.quotient = n1/n2; 
      return mr; 
     } 
    } 

    [DataContract] 
    public class MathResult 
    { 
     [DataMember] 
     public double sum; 

     [DataMember] 
     public double difference; 

     [DataMember] 
     public double product; 

     [DataMember] 
     public double quotient; 
    } 
} 

下一頁控制檯應用程序代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.ServiceModel; 

namespace WCFServiceHost 
{ 
    class Program 
    { 
     public static void Main() 
     { 
      var adrs = new Uri[1]; 
      adrs[0] = new Uri("http://localhost:3980"); 
      using (ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService), adrs)) 
      { 
       try 
       { 
        // Open the ServiceHost to start listening for messages. 
        serviceHost.Open(); 

        // The service can now be accessed. 
        Console.WriteLine("The service is ready."); 
        Console.WriteLine("Press <ENTER> to terminate service."); 
        Console.ReadLine(); 

        // Close the ServiceHost. 
        serviceHost.Close(); 
       } 
       catch (TimeoutException timeProblem) 
       { 
        Console.WriteLine(timeProblem.Message); 
        Console.ReadLine(); 
       } 
       catch (CommunicationException commProblem) 
       { 
        Console.WriteLine(commProblem.Message); 
        Console.ReadLine(); 
       } 
      } 
     } 
    } 
} 

我的2個問題:

1. 當我打開_http://本地主機:3980我得到了: enter image description here如何啓用元數據發佈?請參閱戴博克的答案。

  1. 現在如何從這個服務中獲取數據 - (例如從msdn例子中獲取服務的數據)?嵌入任何Web框架(如Nancy)或使用HttpListener?

回答

1

你需要確保你WCF web配置設置正確

您將需要啓用metat數據HTTP獲取,檢查你的web配置在 system.serviceModel - >行爲 - > serviceBehaviors - >性能 - > serviceMetadata

,並確保您有:

<serviceMetadata httpGetEnabled="true"/> 

第2部分,你可以得到的數據,你可以這樣做

public MathResult GetResult(int a, int b) { 
     var status = new MathResult(); 
     try { 
        var myBinding = new WSHttpBinding(); 
        var myEndpoint = 
         new EndpointAddress(
          new Uri("http://localhost:3980/")); 
        var myChannelFactory = new ChannelFactory<ICalculator>(myBinding, myEndpoint); 
        ICalculator client = myChannelFactory.CreateChannel(); 
      status = client.DoMathJson(a,b); 
     } catch (Exception e) { 
      //do something proper here 
     } 
     return status; 
    } 
+0

謝謝!我的第一個問題是關閉的。在第二個問題中,我的意思是用戶加載html頁面,並使用像http://msdn.microsoft.com/en-us/library/bb472488.aspx中的服務。 – amaranth

+0

你的意思是你在使用南希?這個例子可以放在一個庫中,並按照上面的方式使用。你迄今爲止做了什麼努力。你有一些示例代碼? –

+0

目前我不使用南希或其他框架,我想知道可能不使用任何庫的主機HTML與XHR請求WCF服務? – amaranth

0

你的第一個問題解決了: 但如果將兩者一起會更好。 它用於元數據生成:

第2部分:爲了獲取數據,客戶端,您使用HttpWebRequest和從客戶調用服務。

0

現在如何從這項服務

,從要得到這個數據取決於獲取數據。從客戶端呈現的網頁上,您可以使用jQuery Ajax的一些功能。

如果你想從服務器端消費這個,你可以使用HttpWebRequest或類似的東西。

+0

即我可以從任何HTML頁面(例如,從本地磁盤打開)通過使用XHR調用我的WCF服務方法並獲取數據,如http://msdn.microsoft.com/en-us/ library/bb472488.aspx?謝謝 – amaranth

+0

也許,你有沒有試過? – CodeCaster

+0

不,我不知道從哪一方開始:( – amaranth

相關問題