wcf服务无法激活

时间:2010-02-04 13:53:26

标签: iis-7 wcf

这是一个带有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>

5 个答案:

答案 0 :(得分:8)

如果您在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"
%>

答案 1 :(得分:1)

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

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

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

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

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

答案 2 :(得分:1)

问题是由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);
        }
    }

仍然感觉很乱,应该由微软解决。

答案 3 :(得分:0)

尝试在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

答案 4 :(得分:0)

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

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

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

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

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

  5. 右键单击项目 - &gt;添加 - &gt;服务参考

  6. 点击发现 - &gt;在服务选择服务下 - &gt;为您的服务参考提供所需的名称 - &gt;点击确定

  7. 它将创建&#34; ServiceReference1&#34;在文件夹&#34;服务参考&#34; &安培;自动创建/更新app.config文件 enter image description here