从WCF服务获取实体实体、WCF

2023-09-06 18:56:00 作者:嘴角輕揚、笑看人世冷暖

我有一个问题,从一个WCF服务获取的对象。 我创建了一个WCF项目和一个Silverlight项目。 在SQL EX preSS我有两个表的数据库。QuestionSet和AnswerSet

i have a problem to get an object from a WCF Service. I created a WCF project and a Silverlight project. in SQL express i have a database with two tables: QuestionSet and AnswerSet.

所以,我的WCF项目,我加了我的数据库模型中添加一个新项目时,ADO.NET实体数据模型。然后,我在那里的code代项目(ADO.NET的DbContext发生器)creted。 (即时通讯使用实体框架4.1)。

So, i the WCF project i added my database model by adding a new item, the ADO.NET Entity Data Model. Then i creted from there an Code Generation Item (ADO.NET DbContext Generator). (Im using Entity Framework 4.1).

通过这一点,有创建了三个班的Context.tt,AnswerSet,QuestionSet和Context.cs

With this, there are created three classes in the Context.tt, AnswerSet, QuestionSet and Context.cs

在QuestionSet类看起来如下:

the QuestionSet class looks as follows:

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; }
}}

我的服务看起来就像是:

My Service looks like that:

    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类,并投了..但为什么心不是从数据库中创建实体的工作? ..

and when i now start the service, the WCF test client starts. The method doWork works fine with my own created class. but the other method, doWorkQuest works not. it works when i change the return value to a DTO class and cast that.. but why isnt working with the entity created from the database?? ..

这是WCF客户端显示了错误:

this is the error that the wcf client shows:

无法调用服务。可能的原因:该服务处于脱机状态或无法访问;客户端配置不匹配   代理;现有的代理是无效的。请参考堆栈跟踪   更多的细节。您可以尝试通过启动一个新的代理来恢复,恢复   默认配置,或刷新该服务。

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.

服务器堆栈跟踪:在   System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException   webException,HttpWebRequest的要求,HttpAbortReason abortReason)   在   System.ServiceModel.Channels.HttpChannelFactory.Htt$p$pquestChannel.HttpChannelRequest.WaitForReply(TimeSpan   超时)在   System.ServiceModel.Channels.RequestChannel.Request(消息的消息,   时间跨度超时)在   System.ServiceModel.Dispatcher.RequestChannelBinder.Request(消息   消息,时间跨度超时)在   System.ServiceModel.Channels.ServiceChannel.Call(串动,   布尔单向,ProxyOperationRuntime操作,对象[]插件,   在[对象]出局,时间跨度超时)   System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage   methodCall,ProxyOperationRuntime操作)的   System.ServiceModel.Channels.ServiceChannelProxy.Invoke(即时聊天   消息)

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)

例外重新抛出的[0]:在   System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(即时聊天   reqMsg,即时聊天retMsg)在   System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&安培;   MSGDATA,的Int32型)在INeuralnetworkService.DoWorkQuest()在   NeuralnetworkServiceClient.DoWorkQuest()

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()

内部异常:基础连接已经关闭:连接   被意外关闭。在System.Net.HttpWebRequest.GetResponse()   在   System.ServiceModel.Channels.HttpChannelFactory.Htt$p$pquestChannel.HttpChannelRequest.WaitForReply(TimeSpan   超时)

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)

没有任何人能帮助我吗?

does anybody can help me?

@约翰·桑德斯,感谢您的回答。我接过来一看Windows的事件查看器。

@John Saunders, thank you for your answer. i took a look to the eventviewer of windows.

所以,错误的是:该ASP的兼容性上,我应该打开的或允许或requered :)看到消息:

So, the error is: that the ASP compatibility is on, i should turn of or allow or requered :) see message:

$

    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.. 

我现在又增加了follwing code我的服务:

I added now the follwing code to my service:

    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

但它仍然无法正常工作。

but it still wont work..

我试图安装ASPNET_REGIIS.EXE。但安装检测到错误。查看日志。 但我检查IIS配置,默认应用将和其他人是在.NET Framework 4.0.30319。

i tried to install aspnet_regiis.exe. but the installation detects an Error. See log. But i checked the IIS Configuration, the DefaultAPPPool and others are the .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

事件日志包含以下内容:

the Event LOG contains the following:

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发送实体?但是从它的字符串是可发送的?

But is this normal that i can not send an entity through WCF? but a string from it is sendable?

BR 达米尔

*的的修改的*

*EDIT*

感谢大家对我的帮助。我通过这个问题,并从你的帮助learnd很多。谢谢你。

Thanks to everybody for helping me. I learnd a lot through this problem and from your help. Thank you.

推荐答案

的实体可能仍然在代理格式。通过实体框架自动生成的动态代理不转让以及通过线路。

The entities are probably still in a proxy format. The dynamic proxies automatically generated by Entity Framework do not transfer well over the wire.

要关闭这个功能,在服务上一定要设置ProxyCreatedEnabled为false:

To turn this off, on the service make sure you set ProxyCreatedEnabled to false:

yourContextObject.Configuration.ProxyCreationEnabled = false;