将对象通过WCF服务传递给silverlight

时间:2013-10-28 15:44:20

标签: c# wcf entity-framework silverlight

我的Silverlight应用程序中有一个WCF项目。 WCF项目使用Entity Framework从数据库读取,我想将反映行的对象列表传递给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>

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

我认为问题在于您的服务引用超出了范围。我无法访问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
        }