2014-09-12 124 views
1

我已經研究過這一天了,現在正在轉向您的專家來諮詢您的意見。自行託管WCF無配置 - 難以理解設置端點

我有REST服務是主要的IIS託管服務。

此服務需要與32位非託管的.dll接口。爲了做到這一點,我正在採取一種常用的方法,即創建一個自己託管的服務並打電話給它。

有了這樣的背景,我有一個時間讓自己託管的人工作。

這裏要說的是作品中的代碼:

static void Main(string[] args) 
     { 
      Uri baseAddress = new Uri("http://localhost:8080/theSvc"); 
      Uri mexUri = new Uri("http://localhost:8080/theSvc/mex"); 

      // Create the ServiceHost. 
      using (ServiceHost host = new ServiceHost(typeof(ImgService), baseAddress)) 
      { 
//    var wsHttpBinding = new WSHttpBinding(); 
    //    wsHttpBinding.MaxReceivedMessageSize = int.MaxValue; 
    //   host.AddServiceEndpoint(typeof(IImgService), wsHttpBinding, baseAddress); 

       // Enable metadata publishing. 
       ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); 
       smb.HttpGetUrl = mexUri; 
       smb.HttpGetEnabled = true; 
       smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15; 
       host.Description.Behaviors.Add(smb); 



       // Open the ServiceHost to start listening for messages. Since 
       // no endpoints are explicitly configured, the runtime will create 
       // one endpoint per base address for each service contract implemented 
       // by the service. 
       host.Open(); 

       Console.WriteLine("The service is ready at {0}", baseAddress); 
       Console.WriteLine("Press <Enter> to stop the service."); 
       Console.ReadLine(); 

       // Close the ServiceHost. 
       host.Close(); 
      } 
     } 
    } 

現在...如果我爲了增加maxreceivedMessageSize從using語句刪除baseAddress取消註釋這些行,我再也不能添加到參考服務:

所以更改此設置:

 using (ServiceHost host = new ServiceHost(typeof(ImgService), baseAddress)) 

要這樣:

 using (ServiceHost host = new ServiceHost(typeof(ImgService))) 

,並取消本:

//    var wsHttpBinding = new WSHttpBinding(); 
    //    wsHttpBinding.MaxReceivedMessageSize = int.MaxValue; 
    //   host.AddServiceEndpoint(typeof(IImgService), wsHttpBinding, baseAddress); 

,我收到的錯誤:

... - >本地主機

There was an error downloading 'http://...:8080/theSvc/_vti_bin/ListData.svc/$metadata'. 
The request failed with HTTP status 400: Bad Request. 
Metadata contains a reference that cannot be resolved: 'http://...:8080/theSvc'. 
Metadata contains a reference that cannot be resolved: 'http://...:8080/theSvc'. 
If the service is defined in the current solution, try building the solution and adding the service reference again. 

像其他人說的帖子在這裏...我處於困境中。任何幫助真的很感激。

+0

您是否考慮過WCF的替代方法,與簡單的HTTP服務相比,這種方法往往比較複雜? – Simone 2014-09-12 19:23:05

+0

如果能夠滿足我的需求,我會100%滿意。 我覺得我和WCF的服務非常接近,如果我能把這個最後的配置部分弄清楚 – tronious 2014-09-12 19:26:38

+1

嗯,你已經被警告過:)真的,我不能幫你,我小心翼翼地避免WCF – Simone 2014-09-12 19:28:48

回答

1

看來您並未添加服務端點。

這是我用來啓動一個http主機的函數,但是我將它託管在一個windows服務中。這與您的控制檯應用程序相同。但IIS可能會有點不同。

static Uri scannerSvcBaseAddress; 
static BasicHttpBinding scannerBinding; 
static ServiceHost scannerHost; 

public void startScannerWcfService(long eventID) 
{ 
    try 
    { 
     db.writeServiceLog(eventID, "STARTUP", "=== Scanner WCF Service Starting ==="); 

     string addressToHostAt = ConfigurationManager.AppSettings["ScannerHostBaseAddress"]; 

     if (addressToHostAt != null && addressToHostAt.Trim() != string.Empty) 
     { 

      scannerSvcBaseAddress = new Uri(addressToHostAt); 
      scannerHost = new ServiceHost(typeof(BarCodeService.ScannerService), scannerSvcBaseAddress); 

      //Allows publishing of METADATA to developers when self hosted on a remote system     
      ServiceMetadataBehavior smb = scannerHost.Description.Behaviors.Find<ServiceMetadataBehavior>(); 
      if (smb == null) 
      { 
       smb = new ServiceMetadataBehavior(); 
      } 
      smb.HttpGetEnabled = true; 
      smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15; 
      scannerHost.Description.Behaviors.Add(smb); 

      scannerHost.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexHttpBinding(), "mex"); 

      scannerBinding = new BasicHttpBinding(); 
      scannerBinding.MaxReceivedMessageSize = 2147483647; 
      scannerBinding.Security.Mode = BasicHttpSecurityMode.None; 
      scannerBinding.OpenTimeout = new TimeSpan(0, 0, 10); 
      scannerBinding.CloseTimeout = new TimeSpan(0, 0, 10); 
      scannerBinding.SendTimeout = new TimeSpan(0, 5, 0); 
      scannerBinding.ReceiveTimeout = new TimeSpan(0, Global.scanUserIdleLogout * 2, 0); 

      //scannerBinding.ReliableSession.InactivityTimeout = new TimeSpan(2, 0, 0, 0); 

      scannerHost.AddServiceEndpoint(typeof(BarCodeService.IScannerService), scannerBinding, ""); 

      var behavior = scannerHost.Description.Behaviors.Find<ServiceDebugBehavior>(); 
      behavior.IncludeExceptionDetailInFaults = true; 


      scannerHost.Open(); 

      db.writeServiceLog(eventID, "STARTUP", "=== Scanner WCF Service Started ==="); 
     } 
     else 
      throw new Exception("Host Base Address not provided"); 
    } 
    catch (Exception ex) 
    { 
     db.writeServiceLog(eventID, "STARTUP", string.Format("Error in ServiceManager.startScannerWcfService: {0} {1}", ex.Message, ex.StackTrace)); 
     throw; 
    } 

} 

所有這一切,如上所述,如果你只需要暴露一個字符串WCF可能會矯枉過正。我在Windows服務中使用WCF連接到倉庫環境中的手持掃描儀,我對它的表現非常滿意。雖然最初讓它工作是一個巨大的痛苦。

但是由於問題是在WCF的上下文中提出的,我以爲我會用一個已知的工作函數做出響應,這個函數基本上做了你想做的事情。

+0

丁丁丁丁贏家雞晚餐! 我愛StackOverflow。 非常感謝您抽出寶貴時間。這似乎解決了我的問題。 現在我只需要逐行閱讀並理解原因。 – tronious 2014-09-12 20:05:43