WCF REST API -使用REST API“值”不能为空时出错.参数名称:key:

本文关键字:API REST 出错 参数 key 不能 使用 WCF | 更新日期: 2023-09-27 18:18:51

我创建了一个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配置:

    <?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>

WCF REST API -使用REST API“值”不能为空时出错.参数名称:key:

您没有为您的服务定义一个等待的基址。

<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>