简体   繁体   English

WCF Visual Studio 2017 本地开发

[英]WCF Visual Studio 2017 local development

March 27, 2020 UPDATE 2020 年 3 月 27 日更新

It has been 4 days and I have padded my office walls now.已经 4 天了,我现在已经把我的办公室墙填满了。 :) :)

BIG QUESTION大问题

This will only work locally if I change the following settings Anonymous Authentication = Enabled如果我更改以下设置,这将仅在本地工作 匿名身份验证 = 已启用

Does anybody know how to get around this issue with - IIS Express - Visual Studio 2017有谁知道如何解决这个问题 - IIS Express - Visual Studio 2017

I stepped out of the work code and created a test locally and I am only stuck on one issue now.我退出了工作代码并在本地创建了一个测试,现在我只停留在一个问题上。

Authentication is now my BLOCKER身份验证现在是我的拦截器

I will need to use the settings for deployment as I do not have any control on the DEV |我将需要使用设置进行部署,因为我对 DEV 没有任何控制权 | SIT |坐 | UAT | UAT | PROD --- IAAS or PAAS - I can only code CI and CD. PROD --- IAAS 或 PAAS - 我只能编码 CI 和 CD。

I removed all the ---- Configuration This was not in the original source code我删除了所有----配置原来的源代码中没有这个

I do have to comment out the code in the我必须注释掉

MultipleBindingServiceHost.cs file MultipleBindingServiceHost.cs 文件

The localhost is complaining about the 2 URLs (I will revisit once I get all the security holes in the code fixed.本地主机抱怨 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


March 23, 2020 2020 年 3 月 23 日

I have been trying to figure out WCF and Local Machine development for the past week and now I am coming to stackoverflow for the communities assistance.过去一周我一直在努力弄清楚 WCF 和本地机器的开发,现在我来stackoverflow寻求社区帮助。

I have been tasked with supporting an application that has two WCF Services我的任务是支持具有两个 WCF 服务的应用程序

Web.config appSettings Settings as follows: 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 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>

I keep getting the following error:我不断收到以下错误:

The value could not be added to the collection, as the collection already contains an item of the same type: 'System.ServiceModel.Description.ServiceMetadataBehavior'.无法将该值添加到集合中,因为该集合已包含相同类型的项:“System.ServiceModel.Description.ServiceMetadataBehavior”。 This collection only supports one instance of each type.此集合仅支持每种类型的一个实例。 Parameter name: item参数名称:项目

This points me to the MultipleBindingServiceHost.cs file这将我指向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;
            }
        }
    }

I know I am very close and probably missing something very simple, but after spending multiple days on this I have to post on stackoverflow to get the services running on my local maching.我知道我非常接近并且可能遗漏了一些非常简单的东西,但是在花了多天时间之后,我必须在 stackoverflow 上发帖以使服务在我的本地机器上运行。

WCF service application project doesn't support multiple service contracts in one service host. WCF 服务应用程序项目不支持在一个服务宿主中使用多个服务契约。 One service project has one service host, we need to specify the service implemented class during starting the host.一个服务项目有一个服务宿主,我们需要在启动宿主时指定服务实现的类。 Consequently, it is impossible to host multiple service contracts in one host.因此,不可能在一台主机上托管多个服务合同。 We could create multiple hosts in a console/Windows NT service application so as to support multiple service contracts.我们可以在控制台/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();
}

Feel free to let me know if there is anything I can help with.如果有什么我可以帮忙的,请随时告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM