1

我的 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>
4

2 回答 2

0

我认为问题在于您的服务参考超出了范围。我无法访问视觉工作室,但你想做这样的事情。

//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
        }
于 2013-10-28T20:33:39.977 回答
0

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

[DataContract]
[Serializable]
public class Cat
{
[DataMember]
public int Age {get;set;}
}
于 2013-10-28T16:36:14.167 回答