繁体   English   中英

wcf服务无法激活

[英]wcf Service could not be activated

它是一个带有ssl和成员资格的.svc IIS托管服务。

我的wcf客户报告:

System.ServiceModel.ServiceActivationException was unhandled
  Message="The requested service, 'https://www.greenjump.nl/WebServices/OrderService.svc' could not be activated. See the server's diagnostic trace logs for more information."
  Source="mscorlib"

在我得到的服务器上:System.ArgumentException此集合已包含方案http的地址。 此集合中每个方案最多只能有一个地址。 参数名称:item

奇怪的是,这只发生在生产服务器上,localhost开发服务器上的相同代码和配置工作正常。 我只更改端点地址,并从计算机名称更改为www.webdomain.com

更多服务器跟踪

<ExceptionType>
  System.ArgumentException, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
</ExceptionType>
<Message>
  This collection already contains an address with scheme http.  There can be at most one address per scheme in this collection.
  Parameter name: item
</Message>
<StackTrace>
  at System.ServiceModel.UriSchemeKeyedCollection.InsertItem(Int32 index, Uri item)
  at System.Collections.Generic.SynchronizedCollection`1.Add(T item)
  at System.ServiceModel.UriSchemeKeyedCollection..ctor(Uri[] addresses)
  at System.ServiceModel.ServiceHost..ctor(Type serviceType, Uri[] baseAddresses)
  at SharpShop.Web.StructureMap.StructureMapServiceHost..ctor(Type serviceType, Uri[] baseAddresses)
  at SharpShop.Web.StructureMap.StructureMapServiceHostFactory.CreateServiceHost(Type serviceType, Uri[] baseAddresses)
  at System.ServiceModel.Activation.ServiceHostFactory.CreateServiceHost(String constructorString, Uri[] baseAddresses)
  at System.ServiceModel.ServiceHostingEnvironment.HostingManager.CreateService(String normalizedVirtualPath)
  at System.ServiceModel.ServiceHostingEnvironment.HostingManager.ActivateService(String normalizedVirtualPath)
  at System.ServiceModel.ServiceHostingEnvironment.HostingManager.EnsureServiceAvailable(String normalizedVirtualPath)
  at System.ServiceModel.ServiceHostingEnvironment.EnsureServiceAvailableFast(String relativeVirtualPath)
  at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.HandleRequest()
  at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.BeginRequest()
  at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.OnBeginRequest(Object state)
  at System.ServiceModel.PartialTrustHelpers.PartialTrustInvoke(ContextCallback callback, Object state)
  at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.OnBeginRequestWithFlow(Object state)
  at System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.WorkItem.Invoke2()
  at System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.WorkItem.Invoke()
  at System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.ProcessCallbacks()
  at System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.CompletionCallback(Object state)
  at System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.ScheduledOverlapped.IOCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
  at System.ServiceModel.Diagnostics.Utility.IOCompletionThunk.UnhandledExceptionFrame(UInt32 error, UInt32 bytesRead, NativeOverlapped* nativeOverlapped)
  at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
</StackTrace>

配置:

<system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="wsHttps">
          <readerQuotas maxStringContentLength="128000"/>
          <security mode="TransportWithMessageCredential">
            <transport clientCredentialType="None"/>
            <message clientCredentialType="UserName"/>
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" >
      <baseAddressPrefixFilters>
        <add prefix="https://www.greenjump.nl"/>
      </baseAddressPrefixFilters>
    </serviceHostingEnvironment>
    <behaviors>
      <serviceBehaviors>
        <behavior name="WsHttpWithAuthBehavior">
          <serviceMetadata httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
          <serviceAuthorization principalPermissionMode="UseAspNetRoles"
                                roleProviderName="AspNetSqlRoleProvider"/>
          <serviceCredentials>
            <userNameAuthentication userNamePasswordValidationMode="MembershipProvider"
              membershipProviderName="AspNetSqlMembershipProvider" />
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="WsHttpWithAuthBehavior" name="SharpShop.Services.OrderService">
        <endpoint address="https://www.greenjump.nl/WebServices/OrderService.svc" 
                  binding="wsHttpBinding"
                  bindingConfiguration="wsHttps"
                  contract="SharpShop.Services.IOrderService">
          <identity>
            <dns value="www.greenjump.nl" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />
      </service>
    </services>
  </system.serviceModel>

如果您在IIS中托管,则不需要基本地址部分 - 因为它是提供它的IIS的站点配置 - 因此请删除该部分。

虽然为多个主机头配置了IIS,但存在“问题”,在这种情况下,您需要使用自定义工厂删除除所需地址之外的所有地址。 一个简单的例子是

namespace Example
{
    public class GenericServiceHostFactory : ServiceHostFactory
    {
        protected override ServiceHost CreateServiceHost(Type serviceType, 
                                                         Uri[] baseAddresses)
        {
            //return the first...
            return new ServiceHost(serviceType, baseAddresses[0]);
        }
    }
} 

然后,您将在.svc文件中使用它

<%@ ServiceHost 
    Service="MyImplementationClass"
    Factory="Example.GenericServiceHostFactory"
%>

两个猜测:你在那里有多个<endpoint address =“”条目。

它们可能没有相同的值,但它们可能会解析为相同的名称。

或者因为看起来你正在使用https://,你是否在开发机器上使用http而在实时机器上使用https?

如果是这样,您是否有两个单独的端点地址? 理论上你不应该这样做 - 你在http基地址上启用传输安全性,这将阻止它在除https以外的任何其他地方被调用。

从技术上讲,http和https都是http方案。

该问题是由IIS处理多个主机头引起的。 像blowdart这样说。 错误:此集合已包含带有方案http的地址。

在这里更详细一点http://forums.silverlight.net/forums/p/12883/274592.aspx

使用第一个baseAddresses [0]对我来说不是一个选项,因为我的baseAddresses是http://localhost/WebServices/OrderService.svc http://www.greenjump.nl/WebServices/OrderService.svc https://vps2051.xlshosting .net / WebServices / OrderService.svc按此顺序我当然可以[1],但我不喜欢这种配置依赖。

由于https绑定,我的问题似乎有点复杂,这是我提出的ServiceHostFactory:

 public class MyServiceHostFactory : ServiceHostFactory
    {

        protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {
            Uri webServiceAddress = baseAddresses[0]; //default to first

            if (HttpContext.Current != null) //this only works when aspNetCompatibilityEnabled=true
            {
                string host = HttpContext.Current.Request.Url.Host;
                var goodAddress = baseAddresses.FirstOrDefault(a => a.Host == host);//try to find address with same hostname as request
                if(goodAddress!=null)
                {
                    webServiceAddress = goodAddress;
                }
                Type[] sslServices = { typeof(OrderService)  };//add all https services here
                if (sslServices.Any(s => s == serviceType))
                {
                    UriBuilder builder = new UriBuilder(webServiceAddress);
                    builder.Scheme = "https";
                    builder.Port = 443; //fails if you use another port
                    webServiceAddress = builder.Uri;
                }
            }
            return new ServiceHost(serviceType, webServiceAddress);
        }
    }

这仍然感觉很糟糕,应该由微软解决。

尝试在web配置中为生产服务器添加前缀:

<serviceHostingEnvironment>
  <baseAddressPrefixFilters>
    <add prefix="https://www.greenjump.nl:443" />
  </baseAddressPrefixFilters>
</serviceHostingEnvironment>

这将添加到<system.serviceModel>

您的端点应如下所示:

   <endpoint address="https://www.greenjump.nl:443/WebServices/OrderService.svc" 
              binding="wsHttpBinding"
              bindingConfiguration="wsHttps"
              contract="SharpShop.Services.IOrderService">

否则,您可以像其他评论员建议的那样重载ServiceFactory

我可以通过使用Visual Studio 2013执行以下步骤来解决此问题:

  1. 转到您拥有* .svc文件的项目文件夹

  2. 右键单击* .svc文件 - >在浏览器中查看

  3. 验证您是否能够浏览该服务

  4. 转到您拥有app.config文件的项目文件夹或要使用该服务的位置。

  5. 右键单击项目 - >添加 - >服务参考

  6. 单击发现 - >在服务选择服务下 - >为服务引用指定所需名称 - >单击确定

  7. 它将在“服务引用”文件夹下创建“ServiceReference1”并自动创建/更新app.config文件 在此输入图像描述

暂无
暂无

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

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