gpt4 book ai didi

c# - HttpChannelRequest WaitForReply 上的 WCF 代理 ProcessGetResponseWebException

转载 作者:行者123 更新时间:2023-11-30 16:24:12 42 4
gpt4 key购买 nike

服务合约

[OperationContract]
List<Campaign> AttainCampaigns();

在直接实例化对象时有效

public List<Campaign> AttainCampaigns()
{
var campaign1 = new Campaign { Id = "x", Name = "namex" };
var campaign2 = new Campaign { Id = "y", Name = "namey" };
var campaigns = new List<Campaign> { campaign1, campaign2 };
return campaigns;
}

如果来自 Entity Framework 的等效对象,客户端会抛出错误

// Different implementation getting the campaign objects from database
private List<Campaign> RetrieveCampaigns()
{
return Global.Repositor.Campaigns.Select(c => c).ToList();;
}

public static EntityRepositor Repositor
{
get
{
if (_entityRepositor == null)
_entityRepositor = new EntityRepositor();
return _entityRepositor;
}
}

public class EntityRepositor : DbContext
{
public DbSet<Campaign> Campaigns { get; set; }
}

服务器上没有明显的错误

  • 奇怪的是,当我将调试器附加到服务器端的进程时,我没有看到任何异常 - Entity Framework 似乎很好地实例化了底层 SQL Express 数据库中的对象
  • 我什至没有在输出中看到任何第一次机会异常

大约 15 秒后 WCF 测试客户端异常,然后立即

  • 我第一次调用 AttainCampaigns() 客户端等待/处理大约 15 秒,然后在下面显示异常
  • 所有后续调用都会立即显示错误,即使我删除服务并重新连接也是如此

_我的猜测是数据库调用本身没问题,但它花费的时间太长,所以我需要在服务器或客户端配置中增加某些超时值?如果那是正确的树下吠叫,它们可能是哪些超时?

An error occurred while receiving the HTTP response to http://example.com/Service.svc. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details.
Server stack trace:
at System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
at IBacker.AttainCampaigns()
at BackerClient.AttainCampaigns()
Inner Exception:
The underlying connection was closed: An unexpected error occurred on a receive.
at System.Net.HttpWebRequest.GetResponse()
at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
Inner Exception:
Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.PooledStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead)
Inner Exception:
An existing connection was forcibly closed by the remote host
at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)

服务器 - perSession ServiceContract & Web.config

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, 
ConcurrencyMode = ConcurrencyMode.Single)]

&

  <system.web>
<compilation targetFramework="4.5" />
</system.web>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="wsHttp" allowCookies="true">
<security mode="None" />
</binding>
</wsHttpBinding>
</bindings>
<services>
<service name="MyNameSpace.MyImplementation">
<endpoint address="" binding="wsHttpBinding" bindingConfiguration="wsHttp"
name="wsHttpEp" contract="MyNameSpace.IServiceContract" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<directoryBrowse enabled="true" />
</system.webServer>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
</entityFramework>

客户端 - ChannelFactory & App.config

_channelFactory = new ChannelFactory<T>("wsHttpEp");
Service = _channelFactory.CreateChannel();

&

  <system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="wsHttp" allowCookies="true">
<security mode="None" />
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://example.com/Service.svc" contract="MyNameSpace.IServiceContract"
binding="wsHttpBinding" bindingConfiguration="wsHttp" name="wsHttpEp" />
</client>
</system.serviceModel>

最佳答案

您需要更新服务器配置文件才能在客户端中查看异常详细信息:

<serviceBehaviors>
<behavior name="EmployeeManager_Behavior">
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>

请在启用该选项的情况下发布更新的异常详细信息。

看看您的代码,我可能会给您一个建议。不要将 DbContext 存储为静态变量。 EntityFramework 做了很多缓存,所以上下文实例化没有成本。静态 var 可以由某人处理,Null 检查不会捕获到它。

关于c# - HttpChannelRequest WaitForReply 上的 WCF 代理 ProcessGetResponseWebException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10962761/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com