使用 WCF 数据服务 EF,无法 SaveChange()

Consuming WCF Data Service EF , Failed to SaveChange()

我正在按以下方式使用 WCF 数据服务:

 DataMan.ContextWrapper context = new DataMan.ContextWrapper(new Uri("http://localhost:2060/PCM/DataMan.svc/rest/"));
            DataMan.Report newReport = DataMan.Report.CreateReport("123123123123", DateTime.Now, "999.199905171156550187000.25");

            newReport.Title = "tt";
            newReport.StudyAcDate = Convert.ToDateTime("2016-05-04 12:09:00");
            newReport.Body = "asdasd";
            newReport.Auther = "ali.h";
            newReport.ApproverComment = "cm";
            newReport.Approver = "admin";
            context.AddToReports(newReport);
            DataServiceResponse response = context.SaveChanges();

但是在调用 SaveChange() 之后我得到了以下错误:

The server encountered an error processing the request. The exception message is 'Incoming message for operation 'ProcessRequestForMessage' (contract 'IRequestHandler' with namespace 'http://tempuri.org/') contains an unrecognized http body format value 'Xml'. The expected body format value is 'Raw'. This can be because a WebContentTypeMapper has not been configured on the binding. See the documentation of WebContentTypeMapper for more details.'. See server logs for more details.

我的 WCF 数据服务如下:

 public class ContextWrapper : DataAccessDbContext
{
    public ContextWrapper() : base("connection string")
    {

    }
}

[JSONPSupportBehavior]
public class DataMan : EntityFrameworkDataService<ContextWrapper>
{
    public static void InitializeService(DataServiceConfiguration config)
    {
        config.SetEntitySetAccessRule("*", EntitySetRights.All);
        config.SetEntitySetAccessRule("Studies", EntitySetRights.None);
        config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
        config.UseVerboseErrors = true; // TODO - Remove for production?
        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
    }
    protected override void HandleException(HandleExceptionArgs args)
    {
        base.HandleException(args);
    }
}

我还实现并配置了 WebContentTypeMapper 以绕过上述错误,如下所示:

 public class ContentTypeMapper : WebContentTypeMapper
{
    public override WebContentFormat GetMessageFormatForContentType(string contentType)
    {
        return WebContentFormat.Raw;
    }
} 

自定义绑定:

   <binding name="XmlMapper">
      <webMessageEncoding webContentTypeMapperType="MARCO.SOA.PCMServiceLib.ContentTypeMapper,MARCO.SOA.PCMServiceLib.Core"/>
      <httpTransport manualAddressing="true"/>
    </binding>
  </customBinding>

服务端点:

 <service behaviorConfiguration="Commom2.Behavior" name="MARCO.SOA.PCMServiceLib.DataMan">
    <endpoint address="rest" behaviorConfiguration="Rest.Behavior" binding="webHttpBinding" 
              bindingConfiguration="XmlMapper" contract="System.Data.Services.IRequestHandler">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:2060/PCM/DataMan.svc"/>
      </baseAddresses>
    </host>
  </service>

但仍然出现异常,我认为我的配置出了问题。

任何帮助将不胜感激。

提前致谢。

好吧,千辛万苦终于解决了这个问题, 所以我们需要为 serviceActivation

启动 factory 属性

所以我的亲戚地址是:

<serviceHostingEnvironment>
  <serviceActivations>
.
.
.
   <add relativeAddress="DataMan.svc" service="MARCO.SOA.PCMServiceLib.DataMan"/>
.
.
.
  </serviceActivations>
</serviceHostingEnvironment>

我把它改成了

<serviceHostingEnvironment>
  <serviceActivations>
.
.
.
    <add relativeAddress="DataMan.svc" service="MARCO.SOA.PCMServiceLib.DataMan" factory="System.Data.Services.DataServiceHostFactory, Microsoft.Data.Services, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
.
.
.
  </serviceActivations>
</serviceHostingEnvironment>

现在一切正常。

有关 DataServiceHostFactory

的更多信息

注意: 通过这种方式,我们不需要覆盖 WebContentTypeMapperGetMessageFormatForContentType 并将其强制为 return WebContentFormat.Raw 或其他内容格式,并且不需要任何 customBinding配置文件。

感谢大家。