是否可以在没有IIS的情况下使用Silverlight RiaServices?

时间:2010-12-16 15:43:09

标签: silverlight ria wcf-ria-services partial-trust

我想使用silverlight作为我的Windows服务界面。为此,我使用自定义Web服务器来提供xap文件,它工作正常。

现在我想使用RiaServices,但当然我没有涉及IIS。

这是我的代码:

[EnableClientAccess]
public class TestDomainService : DomainService {

    public IQueryable<Foo> GetPontos() {
        List<Foo> list = new List<Foo>();
        list.Add(new Foo {Id = 1});
        return list.AsQueryable();
    }
}

public class Foo {
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
}

和节目:

static void Main(string[] args) {      
      DomainServiceHost host = new DomainServiceHost(typeof(TestDomainService), new Uri("http://0.0.0.0:8099/TestDomainService"));
      host.Open();
}

您可以在空的cmd应用程序中使用此代码,一旦点击播放,就会抛出运行时异常:

System.TypeAccessException未处理Message =尝试安全透明方法'System.ServiceModel.DomainServices.Server.DomainTypeDescriptionProvider.GetForeignKeyMembers()'访问安全关键类型System.ComponentModel.DataAnnotations.AssociationAttribute'失败。 程序集'System.ComponentModel.DataAnnotations,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35'是有条件的APTCA程序集,在当前的AppDomain中未启用。为了使该组件由部分信任或安全性的透明的代码中使用,请在创建应用程序域时添加组件名称“System.ComponentModel.DataAnnotations,公钥= 0024000004800000940000000602000000240000525341310004000001000100B5FC90E7027F67871E773A8FDE8938C81DD402BA65B9201D60593E96C492651E889CC13F1415EBB53FAC1131AE0BD333C5EE6021672D9718EA31A8AEBD0DA0072F25D87DBA6FC90FFD598ED4DA35E44C398C454307E8E33B8426143DAEC9F596836F97C8F74750E5975C64E2189F45DEF46B2A2B1247ADC3652BF5C308055DA9”到的PartialTrustVisibleAssemblies列表。   来源= System.ServiceModel.DomainServices.Server   类型名=“”   堆栈跟踪:        在System.ServiceModel.DomainServices.Server.DomainTypeDescriptionProvider.GetForeignKeyMembers()        在System.ServiceModel.DomainServices.Server.DomainTypeDescriptionProvider.GetTypeDescriptor(Type objectType,Object instance)        at System.ComponentModel.TypeDescriptor.TypeDescriptionNode.DefaultTypeDescriptor.System.ComponentModel.ICustomTypeDescriptor.GetProperties()        在System.ComponentModel.TypeDescriptor.GetProperties(Type componentType)        在System.ServiceModel.DomainServices.Server.DomainServiceDescription.AddEntityType(Type entityType)        在System.ServiceModel.DomainServices.Server.DomainServiceDescription.AddQueryMethod(DomainOperationEntry方法)        在System.ServiceModel.DomainServices.Server.DomainServiceDescription.Initialize()        在System.ServiceModel.DomainServices.Server.DomainServiceDescription.CreateDescription(类型domainServiceType)        在System.ServiceModel.DomainServices.Server.DomainServiceDescription。&lt;&gt; c_ DisplayClass8.b _7(类型类型)        在System.Collections.Concurrent.ConcurrentDictionary 2.GetOrAdd(TKey key, Func 2 valueFactory)        在System.ServiceModel.DomainServices.Server.DomainServiceDescription.GetDescription(类型domainServiceType)        在System.ServiceModel.DomainServices.Hosting.DomainServiceHost..ctor(类型domainServiceType,Uri [] baseAddresses)        在PartialTrustTest.Program.Main(String [] args)中的D:\ Users \ carlucci \ Documents \ My Dropbox \ My Dropbox \ Way2 \ PartialTrustTest \ PartialTrustTest \ Program.cs:第10行        在System.AppDomain._nExecuteAssembly(RuntimeAssembly程序集,String [] args)        在System.AppDomain.nExecuteAssembly(RuntimeAssembly程序集,String [] args)        在System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)        在System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()        在System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext,String [] activationCustomData)        在System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)        在System.Activator.CreateInstance(ActivationContext activationContext)        在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()        在System.Threading.ThreadHelper.ThreadStart_Context(对象状态)        at System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean ignoreSyncCtx)        在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态)        在System.Threading.Th

readHelper.ThreadStart()   InnerException:


我尝试将System.ComponentModel.DataAnnotations添加到APTCA,但没有成功:(

我改变了我的应用程序以完全信任,但没有成功:(

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

不仅可以,而且这里有一个完整的代码清单,为RIA提供了Excel PowerPivot可以使用的OData。请记住,您必须关闭visual studio托管过程,或者只是在没有调试的情况下运行。使用PowerPivot时,请记住包含尾部斜杠,以便您的网址为:http://localhost:999/TestDomainService/

using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.ServiceModel.Activation;
using System.ServiceModel.DomainServices.Hosting;
using System.ServiceModel.DomainServices.Server;

namespace ConsoleApplication1
{
       public partial class Program
       {
              [EnableClientAccess]
              public class TestDomainService : DomainService
              {
                     [Query(IsDefault=true)]
                     public IQueryable<Foo> GetAllFoos()
                     {
                           return new Foo[] { new Foo { Id = 1, Name = "Jonathan" } }.AsQueryable();
                     }
              }

              public class Foo
              {
                     [Key]
                     public int Id { get; set; }
                     public string Name { get; set; }
              }

              static void Main(string[] args)
              {
                     var svc = new DomainServiceHost(typeof(TestDomainService), new Uri[] { new Uri("http://localhost:999/TestDomainService") });
                     svc.Description.Behaviors.RemoveAll<AspNetCompatibilityRequirementsAttribute>();

                     var svcDescription = DomainServiceDescription.GetDescription(typeof(TestDomainService));
                     var endpoints = new ODataEndpointFactory().CreateEndpoints(svcDescription, svc);

                     svc.Description.Endpoints.Clear();

                     foreach (var endpoint in endpoints)
                     {
                           svc.Description.Endpoints.Add(endpoint);
                     }

                     svc.Open();

                     Console.WriteLine("Domain service started, press any key to exit.");
                     Console.ReadKey();
              }
       }
}

答案 1 :(得分:0)

您可以在没有IIS的情况下使用RIA服务。在打开之前配置域服务:

DomainServiceHost host = new DomainServiceHost(typeof(DomainService1), uri);
host.Description.Behaviors.Remove<AspNetCompatibilityRequirementsAttribute>();

同时检查exe文件的* .config,因为我记得有一些与IIS相关的设置你必须删除。

在VS的项目属性中,打开&#34; Debug&#34;选项卡并取消选中&#34;启用Visual Studio托管流程&#34;。