gpt4 book ai didi

.net - 从 WCF 服务获取实体

转载 作者:行者123 更新时间:2023-12-04 06:08:54 25 4
gpt4 key购买 nike

我在从 WCF 服务获取对象时遇到问题。
我创建了一个 WCF 项目和一个 Silverlight 项目。
在 SQL express 中,我有一个包含两个表的数据库:QuestionSet 和 AnswerSet。

因此,在 WCF 项目中,我通过添加一个新项目 ADO.NET 实体数据模型来添加我的数据库模型。然后我从那里生成了一个代码生成项(ADO.NET DbContext Generator)。 (我使用 Entity Framework 4.1)。

这样,在 Context.tt、AnswerSet、QuestionSet 和 Context.cs 中创建了三个类

QuestionSet 类如下所示:

public partial class QuestionSet
{
public QuestionSet()
{
this.AnswerSets = new HashSet<AnswerSet>();
}


public int Id { get; set; }

public string Quest { get; set; }

public virtual ICollection<AnswerSet> AnswerSets { get; set; }
}}

我的服务看起来像这样:
    QuestionnairedbEntities db = new QuestionnairedbEntities();

public MyOwnClass DoWork()
{
MyOwnClass n = new MyOwnClass ();
n.Name = "Name of the Class";
return n;
}

public QuestionSet DoWorkQuest()
{
QuestionSetDTO qsd = new QuestionSetDTO();
return db.QuestionSets.Find(11);
}

当我现在启动服务时,WCF 测试客户端启动。 doWork 方法工作正常
用我自己创建的类。但另一种方法,doWorkQuest 不起作用。当我将返回值更改为 DTO 类并强制转换时,它会起作用。但是为什么不使用从数据库创建的实体? ..

这是 wcf 客户端显示的错误:

Failed to invoke the service. Possible causes: The service is offline or inaccessible; the client-side configuration does not match the proxy; the existing proxy is invalid. Refer to the stack trace for more detail. You can try to recover by starting a new proxy, restoring to default configuration, or refreshing the service.

The underlying connection was closed: The connection was closed unexpectedly.

Server stack trace: at System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
at System.ServiceModel.Channels.HttpChannelFactory.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 INeuralnetworkService.DoWorkQuest() at NeuralnetworkServiceClient.DoWorkQuest()

Inner Exception: The underlying connection was closed: The connection was closed unexpectedly. at System.Net.HttpWebRequest.GetResponse() at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)



有人可以帮助我吗?

@John Saunders,感谢您的回答。我看了一下windows的事件查看器。

因此,错误是:ASP 兼容性已打开,我应该关闭或允许或要求 :) 请参阅消息:

$
    The service cannot be activated because it requires ASP.NET compatibility. 
ASP.NET compatibility is not enabled for this application. Either enable ASP.NET
compatibility in web.config or set
thAspNetCompatibilityRequirementsAttribute.AspNetCompatibilityRequirementsMode
property to a value other than Required..

我现在将以下代码添加到我的服务中:
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

但它仍然不起作用..

我试图安装aspnet_regiis.exe。但安装检测到错误。见日志。
但我检查了 IIS 配置,DefaultAPPPool 和其他人是 .NET Framework 4.0.30319。
.....2011-11-07 08:15:57        Success     Getting all client file dirs and paths
2011-11-07 08:15:57 Starting Creating list of client site scripts dirs
2011-11-07 08:15:57 Starting Creating directory: C:\inetpub\wwwroot\aspnet_client
2011-11-07 08:15:57 Failure Creating directory: C:\inetpub\wwwroot\aspnet_client: CreateDirectoryInternal failed with HRESULT 80070003: 'The system cannot find the path specified. '
2011-11-07 08:15:57 Failure Creating list of client site scripts dirs: CreateSiteClientScriptDir failed with HRESULT 80070003: 'The system cannot find the path specified. '
2011-11-07 08:15:57 Failure Setting up client script files for website:*: Setting up client script files for website: failed with HRESULT 80070003: 'The system cannot find the path specified. '
2011-11-07 08:15:57 Starting Starting service: aspnet_state
2011-11-07 08:15:57 Success Starting service: aspnet_state

事件日志包含以下内容:
WebHost failed to process a request.
Sender Information: System.ServiceModel.ServiceHostingEnvironment+HostingManager/62476613
Exception: System.ServiceModel.ServiceActivationException: The service '/NeuralnetworkService.svc' cannot be activated due to an exception during compilation. The exception message is: The service cannot be activated because it does not support ASP.NET compatibility. ASP.NET compatibility is enabled for this application. Turn off ASP.NET compatibility mode in the web.config or add the AspNetCompatibilityRequirements attribute to the service type with RequirementsMode setting as 'Allowed' or 'Required'.. ---> System.InvalidOperationException: The service cannot be activated because it does not support ASP.NET compatibility. ASP.NET compatibility is enabled for this application. Turn off ASP.NET compatibility mode in the web.config or add the AspNetCompatibilityRequirements attribute to the service type with RequirementsMode setting as 'Allowed' or 'Required'.
at System.ServiceModel.Activation.HostedAspNetEnvironment.ValidateCompatibilityRequirements(AspNetCompatibilityRequirementsMode compatibilityMode)
at System.ServiceModel.Activation.AspNetCompatibilityRequirementsAttribute.System.ServiceModel.Description.IServiceBehavior.Validate(ServiceDescription description, ServiceHostBase serviceHostBase)
at System.ServiceModel.Description.DispatcherBuilder.ValidateDescription(ServiceDescription description, ServiceHostBase serviceHost)
at System.ServiceModel.Description.DispatcherBuilder.InitializeServiceHost(ServiceDescription description, ServiceHostBase serviceHost)
at System.ServiceModel.ServiceHostBase.InitializeRuntime()
at System.ServiceModel.ServiceHostBase.OnBeginOpen()
at System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
at System.ServiceModel.ServiceHostingEnvironment.HostingManager.ActivateService(String normalizedVirtualPath)
at System.ServiceModel.ServiceHostingEnvironment.HostingManager.EnsureServiceAvailable(String normalizedVirtualPath)
--- End of inner exception stack trace ---
at System.ServiceModel.ServiceHostingEnvironment.HostingManager.EnsureServiceAvailable(String normalizedVirtualPath)
at System.ServiceModel.ServiceHostingEnvironment.EnsureServiceAvailableFast(String relativeVirtualPath)
Process Name: WebDev.WebServer40
Process ID: 4536

但是,我无法通过 WCF 发送实体是否正常?但是它的一个字符串是可发送的?

br
达米尔

* 编辑*

感谢大家帮助我。通过这个问题和你的帮助,我学到了很多东西。谢谢你。

最佳答案

实体可能仍处于代理格式。 Entity Framework 自动生成的动态代理不能很好地通过网络传输。

要关闭此功能,请确保在服务上将 ProxyCreatedEnabled 设置为 false:

yourContextObject.Configuration.ProxyCreationEnabled = false; 

关于.net - 从 WCF 服务获取实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8015040/

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