【问题标题】:WCF Visual Studio 2017 local developmentWCF Visual Studio 2017 本地开发
【发布时间】:2020-03-20 14:48:10
【问题描述】:

2020 年 3 月 27 日更新

已经 4 天了,我现在已经填充了办公室的墙壁。 :)

大问题

如果我更改以下设置,这只会在本地工作 匿名身份验证 = 已启用

有谁知道如何解决这个问题 - IIS 快递 - 视觉工作室 2017

我退出了工作代码并在本地创建了一个测试,我现在只卡在一个问题上。

身份验证现在是我的BLOCKER

我需要使用这些设置进行部署,因为我对 DEV 没有任何控制权 |坐 |通用汽车 | PROD --- IAAS 或 PAAS - 我只能编码 CI 和 CD。

我删除了所有的----配置 这不在原始源代码中

我确实必须注释掉

中的代码

MultipleBindingServiceHost.cs 文件

本地主机抱怨这 2 个 URL(一旦我修复了代码中的所有安全漏洞,我将重新访问。

string rawUrl = ConfigurationManager.AppSettings["tsRawUrl"];
                ServiceEndpoint endpoint = AddServiceEndpoint(typeof(ITicketService), httpBinding, baseAddress, new Uri(rawUrl));
                endpoint.Behaviors.Add(new WebHttpBehavior());

#if (!DEBUG)
                string vanityUrl = ConfigurationManager.AppSettings["tsVanityUrl"];
                ServiceEndpoint endpoint2 = AddServiceEndpoint(typeof(ITicketService), httpBinding, baseAddress, new Uri(vanityUrl));
                endpoint2.Behaviors.Add(new WebHttpBehavior());
#endif


2020 年 3 月 23 日

过去一周我一直在尝试了解 WCF 和本地机器的开发,现在我来到 stackoverflow 寻求社区帮助。

我的任务是支持具有两个 WCF 服务的应用程序

Web.config appSettings 设置如下:

<appSettings>
    <add key="tsVanityUrl" value="http://localhost:1574/TicketService.svc" />
    <add key="tsRawUrl" value="http://localhost:1574/TicketService.svc" />
    <add key="fsVanityUrl" value="http://localhost:1574/FileService.svc" />
    <add key="fsRawUrl" value="http://localhost:1574/FileService.svc" />
</appSettings>

Web.config system.serviceModel

  <system.serviceModel>
    <!-- START RBD Additions for Local Development -->
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    <behaviors>
      <serviceBehaviors>
        <behavior name="TicketBehavior">
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
        <behavior name="FileBehavior">
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service name="TicketSystem.TicketService" behaviorConfiguration="TicketBehavior"> 
        <endpoint address="/TicketService.svc"                  
                  binding="basicHttpBinding"
                  contract="TicketSystem.ITicketService"
                  />
      </service>
      <service name="TicketSystem.FileService" behaviorConfiguration="FileBehavior"> 
        <endpoint address="/FileService.svc"                  
                  binding="basicHttpBinding"
                  contract="TicketSystem.IFileService" 
                  />
      </service>
    </services>
    <!-- END RBD Additions for Local Development -->
  </system.serviceModel>

我不断收到以下错误:

无法将值添加到集合中,因为集合已包含相同类型的项:“System.ServiceModel.Description.ServiceMetadataBehavior”。此集合仅支持每种类型的一个实例。 参数名称:项目

这将我指向 MultipleBindingServiceHost.cs 文件

protected override void ApplyConfiguration()
        {
            base.ApplyConfiguration();

            ServiceMetadataBehavior mexBehavior = new ServiceMetadataBehavior();
            Description.Behaviors.Add(mexBehavior);

            WebHttpBinding httpBinding = new WebHttpBinding();                                 

            foreach (Uri baseAddress in BaseAddresses)
            {
                if (baseAddress.Scheme == Uri.UriSchemeHttp)
                {
                    httpBinding.Security.Mode = WebHttpSecurityMode.None;
                    mexBehavior.HttpGetEnabled = true;
                }
                else if (baseAddress.Scheme == Uri.UriSchemeHttps)
                {
                    httpBinding.Security.Mode = WebHttpSecurityMode.Transport;
                    httpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
                    mexBehavior.HttpsGetEnabled = true;
                }
                //ServiceEndpoint endpoint = AddServiceEndpoint(typeof(TicketSystem.ITicketService),
                //                        httpBinding,
                //                        baseAddress);
                //endpoint.Behaviors.Add(new WebHttpBehavior());

                //Fix for 404 Vanity URL Issue
                string rawUrl = ConfigurationManager.AppSettings["tsRawUrl"];
                ServiceEndpoint endpoint = AddServiceEndpoint(typeof(ITicketService), httpBinding, baseAddress, new Uri(rawUrl));
                endpoint.Behaviors.Add(new WebHttpBehavior());

                string vanityUrl = ConfigurationManager.AppSettings["tsVanityUrl"];
                ServiceEndpoint endpoint2 = AddServiceEndpoint(typeof(ITicketService), httpBinding, baseAddress, new Uri(vanityUrl));
                endpoint2.Behaviors.Add(new WebHttpBehavior());

                break;
            }
        }
    }

我知道我非常接近并且可能错过了一些非常简单的东西,但是在花了几天时间之后,我必须在 stackoverflow 上发布以让服务在我的本地机器上运行。

【问题讨论】:

    标签: c# wcf


    【解决方案1】:

    WCF 服务应用程序项目不支持在一个服务主机中使用多个服务合同。一个服务项目有一个服务宿主,我们需要在启动宿主时指定服务实现类。因此,不可能在一台主机中托管多个服务合同。我们可以在一个控制台/Windows NT 服务应用程序中创建多个主机,以支持多个服务契约。

    using (ServiceHost sh = new ServiceHost(typeof(TestService)), sh2 = new ServiceHost(typeof(MyService1)))
    {
        sh.Opened += delegate
        {
            Console.WriteLine("service is ready");
        };
    
        sh.Closed += delegate
        {
            Console.WriteLine("service is closed");
        };
        sh2.Opened += delegate
        {
            Console.WriteLine("service2 is ready...");
        };
        sh2.Closed += delegate
        {
            Console.WriteLine("Service is closed...");
        };
        sh.Open();
        sh2.Open();
    
        Console.ReadLine();
        sh.Close();
        sh2.Close();
    }
    

    如果有什么我可以帮忙的,请随时告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-13
      • 1970-01-01
      • 2018-08-23
      • 1970-01-01
      • 1970-01-01
      • 2017-10-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多