繁体   English   中英

WCF错误-不允许使用方法

[英]WCF Error - Method Not Allowed

运行WCF测试客户端并在本地调试WCF服务时,一切正常。 将其部署到远程服务器上的IIS,然后尝试使用WCF测试客户端加载它之后,我开始收到“对象未设置为对象实例”错误。 我向该服务添加了一个try-catch,并且错误更改为“不允许使用方法”。

我整天都在阅读这些错误,大多数帖子都涉及使用JSON或AJAX访问其WCF客户端的人。 我尝试了许多建议的解决方案,其中大部分是对web.config的更改。 在这个阶段,我只是使用测试客户端来访问它,一旦证明可以工作,它将由Winform应用程序使用。

尽管建立了信任关系,但是服务上公开的2种方法都创建了到不同域上的SQL Server的连接。 我认为这可能是问题的一部分。

我的服务界面

[ServiceContract]
public interface IMeterQueryService
{
    [OperationContract]
    List<Meter> FindMeter(string mprn);

实施服务

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class MeterQueryService : IMeterQueryService
{
    public List<Meter> FindMeter(string mprn)
    {
        using (var da = new DataAccess())
        {

        var meters = da.GetMeter(mprn);
        da.Dispose();
        return meters;

        }
    }

应用配置文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" />
  </system.web>
  <connectionStrings>
    <add name="Tims" connectionString="hidden" providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <services>
      <service name="TimsAPI.MeterQueryService">
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, 
          set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="True"/>
          <!-- To receive exception details in faults for debugging purposes, 
          set the value below to true.  Set to false before deployment 
          to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="True" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

Web配置文件,站点引用WCF服务库。

<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="false" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <services>
      <service name="TimsAPI.MeterQueryService">
        <endpoint address="http://easvr33:1000/" binding="basicHttpContextBinding" bindingConfiguration=""
          contract="TimsAPI.IMeterQueryService" />
        <endpoint address="mex" binding="mexHttpBinding" bindingConfiguration=""
          contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="localhost" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="false" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

更新资料

我将WCF服务重新创建为WCF服务网站,而不是图书馆,现在一切正常。 我认为这2个配置文件之间有些奇怪。 我仍然想知道为什么它从未成功过,但我也知道对于没有完全访问源代码的人可能很难诊断

服务SVC可能是一个问题。 这里

1.打开虚拟目录的属性。

2.转到目录选项卡。

3.单击配置。

4.点击添加...

5.提供以下信息:

可执行文件:C:\\ WINDOWS \\ Microsoft.NET \\ Framework \\ v2.0.50727 \\ aspnet_isapi.dll

b。扩展名:.svc

c。动词:GET,HEAD,POST,DEBUG

当通过IIS网站托管WCF服务时,不使用服务app.config ,而是加载宿主站点的web.config 服务正常运行所需的所有设置都必须复制到托管站点的web.config中。

暂无
暂无

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

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