2014-09-12 43 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