WCF REST API - 使用rest API时出错“值不能为null。参数名称:key:

时间:2014-08-20 08:10:23

标签: c# wcf rest azure

我创建了一个WCF REST API并将其发布到Windows Azure。该代码在Azure模拟器中完美运行。但是,当尝试通过浏览器使用API​​ GET方法时,我收到以下错误:

    [ArgumentNullException: Value cannot be null.
    Parameter name: key]
    System.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument) +48
    System.Collections.Generic.Dictionary`2.FindEntry(TKey key) +38
    System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue& value) +20
    System.ServiceModel.Dispatcher.WebHttpDispatchOperationSelector..ctor(ServiceEndpoint    
endpoint) +2459
   System.ServiceModel.Description.WebHttpBehavior.GetOperationSelector(ServiceEndpoint endpoint) +125
   System.ServiceModel.Description.WebHttpBehavior.ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) +1955
   System.ServiceModel.Description.DispatcherBuilder.InitializeServiceHost(ServiceDescription description, ServiceHostBase serviceHost) +4245
   System.ServiceModel.ServiceHostBase.InitializeRuntime() +119
   System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout) +45
   System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +553
   System.ServiceModel.HostingManager.ActivateService(ServiceActivationInfo serviceActivationInfo, EventTraceActivity eventTraceActivity) +350
   System.ServiceModel.HostingManager.EnsureServiceAvailable(String normalizedVirtualPath, EventTraceActivity eventTraceActivity) +1614

[ServiceActivationException: The service '/RestServiceImpl.svc' cannot be activated due to an exception during compilation.  The exception message is: Value cannot be null.
Parameter name: key.]
   System.Runtime.AsyncResult.End(IAsyncResult result) +911
   System.ServiceModel.Activation.HostedHttpRequestAsyncResult.End(IAsyncResult result) +525
   System.ServiceModel.Activation.ServiceHttpModule.EndProcessRequest(IAsyncResult ar) +98
   System.Web.AsyncEventExecutionStep.OnAsyncEventCompletion(IAsyncResult ar) +126

从堆栈跟踪看来,错误似乎与服务的端点有关。 经过更多调查后,从web.config中删除元素时,此错误消失,并发生另一个错误。但是在我看到的每个例子中都出现在web.config中。 这是我的网络配置:

    <?xml version="1.0"?>
    <configuration>
      <system.web>
        <compilation debug="true" targetFramework="4.0" />
        <customErrors mode="Off"/>
      </system.web>
      <system.serviceModel>
        <services> 
          <service name="RestService.RestServiceImpl"     
                   behaviorConfiguration="ServiceBehavior">
          <endpoint address="" 
                    binding="webHttpBinding" 
                    contract="RestService.IRestServiceImpl"                 
                    behaviorConfiguration="web">
          </endpoint>    
        </service>
      </services>
      <behaviors>
          <serviceBehaviors>
            <behavior name="ServiceBehavior">
              <serviceMetadata httpGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>
          </serviceBehaviors>
          <endpointBehaviors>
            <behavior name="web">
              <webHttp />
            </behavior>
          </endpointBehaviors>
       </behaviors>
       <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
     </system.serviceModel>
     <system.webServer>
       <modules runAllManagedModulesForAllRequests="true" />
     </system.webServer>
  </configuration>

1 个答案:

答案 0 :(得分:0)

您没有为服务定义基地址等待。

<services> 
    <service name="RestService.RestServiceImpl"  behaviorConfiguration="ServiceBehavior">
        <host>
            <baseAddresses>
                <add baseAddress="http://localhost:1234/MyWCF/"/>
            </baseAddresses>
        </host>
        <endpoint address="" 
            binding="webHttpBinding" 
            contract="RestService.IRestServiceImpl"                 
            behaviorConfiguration="web">
        </endpoint>    
    </service>
</services>