【问题标题】:Passing an object through WCF service to silverlight通过 WCF 服务将对象传递给 silverlight
【发布时间】:2013-10-28 15:44:19
【问题描述】:

我的 Silverlight 应用程序中有一个 WCF 项目。 WCF 项目使用实体框架从数据库中读取数据,我想将反映行的对象列表传递给 Silverlight 应用程序,这是服务合同;

[ServiceContract]
    public interface IGetToolboxItemsService
    {
        [OperationContract]
        List<Control> GetToolboxItems();

        [OperationContract]
        string ReturnWord(string word);
    }

这是完成工作的类

public class GetToolboxItemsService : IGetToolboxItemsService
    {
        public List<Control> GetToolboxItems()
        {
            SilverlightScreenDesignerEntities ent = new SilverlightScreenDesignerEntities();
            List<Control> controls = new List<Control>();
            controls = ent.Controls.ToList();
            return controls;
        }

        public string ReturnWord(string word)
        {
            return word;
        }
    }

这就是我在客户端调用服务的方式;

ToolboxServiceReference.GetToolboxItemsServiceClient proxy = new ToolboxServiceReference.GetToolboxItemsServiceClient();
            proxy.GetToolboxItemsCompleted += proxy_GetToolboxItemsCompleted;
            proxy.GetToolboxItemsAsync();

然后完成的事件就到这里了;

void proxy_GetToolboxItemsCompleted(object sender, ToolboxServiceReference.GetToolboxItemsCompletedEventArgs e)
        {
            var data = e.Result;
        }

当我调试服务时,它会运行到数据库并带回Controls 的列表,这是预期的,但是当我尝试通过客户端获取控件列表时收到错误消息。

The remote server returned an error: NotFound

然而,当我尝试简单的返回词时,服务工作正常,没有任何中断,是否需要额外的步骤,因为我正在通过一个对象?

这是 web.config;

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <system.web>
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </assemblies>
    </compilation>
  </system.web>
  <system.serviceModel>
  <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    </system.serviceModel>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
  <connectionStrings>
    <add name="ModelContainer" connectionString="metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=MOBINOTE135;initial catalog=BankManager;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    <add name="SilverlightScreenDesignerEntities" connectionString="metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=MOBINOTE135;initial catalog=SilverlightScreenDesigner;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

【问题讨论】:

  • 发布您的网络配置。该服务是否在 WCFClient 中工作?
  • 客户端和调试器是否使用相同的 URL?客户端似乎找不到端点。
  • 添加了网络配置。不,该服务在 WCFClient 中也不起作用,该错误表示“接收到localhost:8733/Design_Time_Addresses/WcfServiceLibrary/… 的 HTTP 响应时发生错误。这可能是由于服务端点绑定未使用 HTTP 协议。这也可能是由于服务器中止了 HTTP 请求上下文(可能是由于服务关闭)。有关详细信息,请参阅服务器日志。'
  • 不,他们不知道@VladimirGondarev 有没有办法纠正这个问题?
  • 我发现在使用 Silverlight 和 WCF 时,最好使用本地 IIS 而不是 IIS Express 或 Cassini。看来您的 ServiceReferences.ClientConfig 具有不同的端点配置端口。

标签: c# wcf entity-framework silverlight


【解决方案1】:

控制是您的用户定义类吗?如果是,您必须标记它以便 wcf 服务可以序列化它:

[DataContract]
[Serializable]
public class Cat
{
[DataMember]
public int Age {get;set;}
}

【讨论】:

    【解决方案2】:

    我认为问题在于您的服务引用超出了范围。我无法访问 Visual Studio,但你想做这样的事情。

    //declare the service outside of the calling routine
    ToolboxServiceReference.GetToolboxItemsServiceClient proxy;
    
    private void GetToolboxItems
    {
    ToolboxServiceReference.GetToolboxItemsServiceClient proxy = new ToolboxServiceReference.GetToolboxItemsServiceClient();
                proxy.GetToolboxItemsCompleted += proxy_GetToolboxItemsCompleted;
                proxy.GetToolboxItemsAsync();
    )
    
    void proxy_GetToolboxItemsCompleted(object sender, ToolboxServiceReference.GetToolboxItemsCompletedEventArgs e)
            {
                var data = e.Result;
                proxy.close //now close the service
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多