C# wpf应用程序中的远程主机强制关闭了现有连接

C# wpf应用程序中的远程主机强制关闭了现有连接,c#,wpf,wcf,exception,C#,Wpf,Wcf,Exception,为了解决这个问题,我在这个网站上引用了很多问题。在我发现的任何地方,我都应该使用如下代码将诊断添加到配置中: <system.diagnostics> <trace autoflush="true" /> <sources> <source name="System.ServiceModel" switchValue="Information, ActivityTracing"

为了解决这个问题,我在这个网站上引用了很多问题。在我发现的任何地方,我都应该使用如下代码将诊断添加到配置中:

<system.diagnostics> 
    <trace autoflush="true" />
    <sources>
      <source name="System.ServiceModel"
              switchValue="Information, ActivityTracing"
              propagateActivity="true">
        <listeners>
          <add name="sdt"
              type="System.Diagnostics.XmlWriterTraceListener"
              initializeData= "D:\SdrConfigExample.e2e" />
        </listeners>
      </source>
    </sources>
  </system.diagnostics>
如果您还需要什么,请说明。我会更新

根据Robert Graves的建议更新2:

我已经将上面代码中提到的诊断部分移动到WCF项目的Web.Config文件中,在那里我收到一条警告,说明未找到配置评估上下文。下面我发现未找到匹配的服务标记。但是我检查了我的web.config文件,发现所有标记都有匹配的结束标记

这是我的Web.Config文件

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="Lab_Lite_Entities" connectionString="metadata=res://*/Lab_Lite_Models.csdl|res://*/Lab_Lite_Models.ssdl|res://*/Lab_Lite_Models.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=VISHAL-PC\SQLEXPRESS;initial catalog=Lab_Lite_Database;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <system.diagnostics>
    <!-- Delete From & To Bookmarks-->
    <trace autoflush="true" />
    <sources>
      <source name="System.ServiceModel"
              switchValue="Information, ActivityTracing"
              propagateActivity="true">
        <listeners>
          <add name="sdt"
              type="System.Diagnostics.XmlWriterTraceListener"
              initializeData= "D:\SdrConfigExample.e2e" />
        </listeners>
      </source>
    </sources>
  </system.diagnostics>
  <system.serviceModel>
    <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="true" minFreeMemoryPercentageToActivateService="0" />
  </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>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

在您的服务中尝试以下方法:

[ServiceContract]
public class Lab_Lite_Service 
{
    [OperationContract]
    public IEnumerable<Patient> GetPatients()
    {
        using (Lab_Lite_Entities db = new Lab_Lite_Entities())
        {
            // note the call of .ToList()
            // you need to materialize the enumerable that is returned, because it's not serializable
            return db.Patients.ToList();
        }
    }
}
[服务合同]
公共课实验室服务
{
[经营合同]
公共IEnumerable GetPatients()
{
使用(Lab_Lite_Entities db=new Lab_Lite_Entities())
{
//注意.ToList()的调用
//您需要具体化返回的可枚举项,因为它不可序列化
返回db.Patients.ToList();
}
}
}

我不确定跟踪侦听器失败的原因,但我在结合EF和WCF时看到的最常见问题是,WCF无法正确序列化EF,因为EF生成代理类。不幸的是,WCF只是关闭了连接,并没有在服务器端生成一个容易捕获的异常。您可以在以下答案中找到更多详细信息:


你的问题是什么?我在标题中提到了一个错误。那么,这对我们意味着什么?服务器不喜欢某些内容并关闭了连接。你想干什么?你的密码在哪里?我们是否必须浏览您的个人资料,查看您之前提出的问题,并根据假设发布答案?好的,我会马上用代码更新我的问题。我认为您已将跟踪信息添加到您的WPF客户端。而是将此配置添加到服务中。您可以发布患者定义吗?如果改为“返回新列表();”,会发生什么?
[ServiceContract]
public class Lab_Lite_Service 
{
    [OperationContract]
    public IEnumerable<Patient> GetPatients()
    {
        using (Lab_Lite_Entities db = new Lab_Lite_Entities())
        {
            // note the call of .ToList()
            // you need to materialize the enumerable that is returned, because it's not serializable
            return db.Patients.ToList();
        }
    }
}