【问题标题】:Get Entity from WCF Service从 WCF 服务获取实体
【发布时间】:2011-11-04 19:58:56
【问题描述】:

我在从 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 客户端显示的错误:

调用服务失败。可能原因:服务离线或无法访问;客户端配置不匹配 代理人;现有代理无效。请参阅堆栈跟踪 更多详情。您可以尝试通过启动新代理来恢复,恢复 到默认配置,或者刷新服务。

底层连接已关闭:连接意外关闭。

服务器堆栈跟踪:在 System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest 请求, HttpAbortReason abortReason)
在 System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(时间跨度 超时)在 System.ServiceModel.Channels.RequestChannel.Request(消息消息, TimeSpan 超时)在 System.ServiceModel.Dispatcher.RequestChannelBinder.Request(消息 消息,TimeSpan 超时)在 System.ServiceModel.Channels.ServiceChannel.Call(字符串动作, Boolean oneway, ProxyOperationRuntime 操作, Object[] ins, Object[] 出局,TimeSpan 超时)在 System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall,ProxyOperationRuntime 操作)在 System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage 留言)

在 [0] 处重新抛出异常:在 System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg,IMessage retMsg)在 System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(消息数据& msgData,Int32 类型)在 INeuralnetworkService.DoWorkQuest() 在 NeuralnetworkServiceClient.DoWorkQuest()

内部异常:底层连接已关闭:连接 意外关闭。在 System.Net.HttpWebRequest.GetResponse() 在 System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(时间跨度 超时)

有人可以帮助我吗?


@John Saunders,感谢您的回答。我看了一下windows的eventviewer。

所以,错误是: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 达米尔


*编辑*

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

【问题讨论】:

  • 服务中出现此问题。查看服务器上的事件日志以了解发生了什么。
  • 约翰,谢谢你..所以我将来知道在发布内容之前我必须先看看哪里..

标签: .net wcf object service entity


【解决方案1】:

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

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

yourContextObject.Configuration.ProxyCreationEnabled = false; 

【讨论】:

  • 谢谢 SeanLAllen.. 这行得通。太好了:)真的谢谢你
  • SeanLAllen,你能解释一下,为什么我必须这样做?为什么要创建代理?深入了解这一点将非常有帮助..
  • 使用POCO实体类型时,通过创建派生代理类型的实例,然后覆盖虚拟属性添加加载钩子来实现延迟加载。 Using the DbContext API
  • 但是我使用“ADO.NET DbContext Generator”是正确的吗?通过这个创建了 3 个类,AnswerSet、QuestionSet 和 Context(它是空的)。然后我总是禁用代理创建?或者只是当我使用 WCF 时?
  • 啊..我明白了..那是因为序列化 :) 因为代理不是普通类型。
【解决方案2】:

你需要安装asp.net。

aspnet_regiis.exe

您应该为 .net 4 或您使用的任何版本查找此过程。

C:\WINDOWS\Microsoft.NET\Framework64\v2.0.50727

【讨论】:

  • 嗨 DarthVader,谢谢你.. 我安装了这个.. 但它仍然无法工作.. 另一个想法? :)
  • 是的,我以管理员身份安装了它。很高兴知道我如何手动安装它。谢谢 DarthVader ..
猜你喜欢
  • 2013-03-26
  • 1970-01-01
  • 2012-02-18
  • 2012-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多