【问题标题】:Is it possible to use Silverlight RiaServices without IIS?是否可以在没有 IIS 的情况下使用 Silverlight RiaServices?
【发布时间】:2010-12-16 15:43:09
【问题描述】:

我想使用 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 未处理 消息=通过安全透明方法“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(类型 objectType,对象实例) 在 System.ComponentModel.TypeDescriptor.TypeDescriptionNode.DefaultTypeDescriptor.System.ComponentModel.ICustomTypeDescriptor.GetProperties() 在 System.ComponentModel.TypeDescriptor.GetProperties(类型 componentType) 在 System.ServiceModel.DomainServices.Server.DomainServiceDescription.AddEntityType(类型 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.c_DisplayClass8.b_7(类型类型) 在 System.Collections.Concurrent.ConcurrentDictionary2.GetOrAdd(TKey key, Func2 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:line 10 在 System.AppDomain._nExecuteAssembly(RuntimeAssembly 程序集,字符串 [] 参数) 在 System.AppDomain.nExecuteAssembly(RuntimeAssembly 程序集,字符串 [] 参数) 在 System.Runtime.Hosting.ManifestRunner.Run(布尔 checkAptModel) 在 System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly() 在 System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext,字符串 [] activationCustomData) 在 System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext 激活上下文) 在 System.Activator.CreateInstance(ActivationContext 激活上下文) 在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone() 在 System.Threading.ThreadHelper.ThreadStart_Context(对象状态) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback 回调,对象状态,布尔 ignoreSyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback 回调,对象状态) 在 System.Threading.Th

readHelper.ThreadStart() 内部异常:


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

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

有什么想法吗?

【问题讨论】:

  • 如果您在调试模式下运行,请务必转到项目属性 -> 调试 -> 取消选中“启用 Visual Studio 托管进程”,您将不再收到此错误。

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


【解决方案1】:

这不仅是可能的,而且这里有一个完整的代码清单,它为 RIA 提供 OData,Excel PowerPivot 可以使用它。请记住,您必须关闭 Visual Studio 托管进程,或者直接运行而不进行调试。使用 PowerPivot 时请记住包含尾部斜杠,这样您的 URL 将是: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();
              }
       }
}

【讨论】:

    【解决方案2】:

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

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

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

    同样在 VS 的项目属性中,打开“调试”选项卡并取消选中“启用 Visual Studio 托管进程”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-30
      • 2014-06-16
      • 2020-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多