在本地运行 WCF 测试客户端并调试 WCF 服务时,一切正常。在我将它部署到远程服务器上的 IIS 并尝试使用 WCF 测试客户端加载它之后,我开始收到“对象未设置为对象实例”错误。我在服务中添加了一个 try-catch,错误更改为“方法不允许”。
我整天都在阅读这些错误,大多数帖子都与使用 JSON 或 AJAX 访问他们的 WCF 客户端的人有关。我尝试了许多建议的解决方案,其中大部分是对 web.config 的更改。在这个阶段我只是使用测试客户端访问它,一旦被证明可以工作,它将被 Winform 应用程序使用。
服务上公开的 2 种方法都创建到不同域上的 SQL 服务器的连接,尽管存在信任设置。我认为这可能是问题的一部分。
我的服务接口
[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 服务网站,而不是库,现在一切正常。我认为这两个配置文件之间有些奇怪。我仍然很想知道为什么它从来没有用过,但我也明白对于没有完全访问源的人可能很难诊断