具有多个合同的Mono WCF Rest Service“在配置文件中未定义端点”

问题描述 投票:1回答:1

我想通过一个单独的部分类中实现的,通过mono托管WCF Rest Service的多个合同。我读过许多关于类似问题的文章,但单声道还没有解决方案。我合并或至少测试了我能找到的所有建议,到现在为止我的代码看起来很像其他解决方案,但是不起作用。

该应用程序在我的本地计算机上成功运行,但是一旦我通过mono部署它,就会引发错误。

服务'MyWebServiceEndpoint'实现多种ServiceContract类型,并且在配置文件中未定义任何端点。

这里是合同的端点之一。所有其他人都非常喜欢这个。它们都是MyWebServiceEndpoint的局部类,实现了另一个协定。

namespace MyServer.MyEndPoints {        
    public partial class MyWebServiceEndpoint : INotificationEndpoint {

        public string GetNotifications(int limit) {
            // Do stuff
        }
    }

    [ServiceContract]
    public interface INotificationEndpoint {

        [OperationContract]
        [WebGet]
        string GetNotifications(int limit);
    }
}

我的App.config看起来像这样。我删除了IP和端口,因为它们是服务器地址。

<system.serviceModel>
<services>
  <service name="MyServer.MyEndPoints.MyWebServiceEndpoint" behaviorConfiguration="WebService.EndPoint">
    <host>
      <baseAddresses>
        <add baseAddress="http://ip:port>"/> 
      </baseAddresses>
    </host>
    <endpoint address="/message"
              binding="webHttpBinding"
              contract="MyServer.MyEndPoints.IMessageEndpoint" 
              behaviorConfiguration="WebBehavior"/>
    <endpoint address="/music"
              binding="webHttpBinding"
              contract="MyServer.MyEndPoints.IMusicEndpoint" 
              behaviorConfiguration="WebBehavior"/>
    <endpoint address="/notification"
              binding="webHttpBinding"
              contract="MyServer.MyEndPoints.INotificationEndpoint"                   
              behaviorConfiguration="WebBehavior"/>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
  </service>
</services>
<behaviors>
  <serviceBehaviors>        
    <behavior name="WebService.EndPoint">
      <serviceMetadata httpGetEnabled="True" />
      <serviceDebug includeExceptionDetailInFaults="True"/>
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="WebBehavior">
      <webHttp/>
    </behavior>
  </endpointBehaviors>
</behaviors>

我像这样用C#打开服务。

WebServiceHost = new WebServiceHost(typeof(MyWebServiceEndpoint));
WebServiceHost.Open();

我在单声道上收到的错误消息是:

Unhandled Exception:
System.InvalidOperationException: Service 'MyWebServiceEndpoint' implements multiple ServiceContract 
types, and no endpoints are defined in the configuration file. WebServiceHost can set up default 
endpoints, but only if the service implements only a single ServiceContract. Either change the 
service to only implement a single ServiceContract, or else define endpoints for the service 
explicitly in the configuration file. When more than one contract is implemented, must add base 
address endpoint manually

我希望您有一些提示,或者有人知道如何解决此问题。已经感谢您阅读这里的内容。

rest wcf configuration mono endpoint
1个回答
0
投票

我不熟悉Mono,Mono支持Webconfig文件吗?我建议您以编程方式添加服务端点。

class Program
    {
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            WebHttpBinding binding = new WebHttpBinding();
            Uri uri = new Uri("http://localhost:21011");
            using (WebServiceHost sh = new WebServiceHost(typeof(TestService),uri))
            {
                sh.AddServiceEndpoint(typeof(ITestService), binding, "service1");
                sh.AddServiceEndpoint(typeof(IService), binding, "service2");
                ServiceMetadataBehavior smb;
                smb = sh.Description.Behaviors.Find<ServiceMetadataBehavior>();
                if (smb == null)
                {
                    smb = new ServiceMetadataBehavior()
                    {
                        HttpGetEnabled = true
                    };

                    sh.Description.Behaviors.Add(smb);

                }

                sh.Opened += delegate
                {
                    Console.WriteLine("service is ready");
                };

                sh.Closed += delegate
                {
                    Console.WriteLine("service is closed");
                };

                sh.Open();

                Console.ReadLine();
                sh.Close();
            }
        }
    }
    [ServiceContract]
    public interface ITestService
    {
        [OperationContract]
        [WebGet]
        string GetData(int id);
    }
    [ServiceContract]
    public interface IService
    {
        [OperationContract]
        [WebGet]
        string Test();
    }

    public class TestService : ITestService,IService
    {
        public string GetData(int id)
        {
            return $"{id},";
        }
        public string Test()
        {
            return "Hello " + DateTime.Now.ToString();
        }
    }

根据官方文档,我们最好不要使用Partial classhttps://docs.microsoft.com/en-us/dotnet/framework/wcf/samples/multiple-contracts此外,我们可以考虑为每个服务实现的类启动多个服务主机。请随时让我知道问题是否仍然存在。

© www.soinside.com 2019 - 2024. All rights reserved.