通过 WCF 服务将对象传递给 silverlight
本文关键字:silverlight 对象 WCF 服务 通过 | 更新日期: 2023-09-27 18:37:20
我的 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
然而,当我尝试简单的返回词时,服务工作正常并且没有任何中断,是否因为 IM 传递对象而需要额外的步骤?
这是网络配置;
<?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="data source=MOBINOTE135;initial catalog=BankManager;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" 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="data source=MOBINOTE135;initial catalog=SilverlightScreenDesigner;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
Control 是您的用户定义的类吗?如果是,则必须对其进行标记,以便 wcf 服务可以序列化它:
[DataContract]
[Serializable]
public class Cat
{
[DataMember]
public int Age {get;set;}
}
我认为问题是您的服务引用超出了范围。我无法访问视觉工作室,但你想做这样的事情。
//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
}