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

4

我想将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.ServiceModel.DomainServices.Server.DomainTypeDescriptionProvider.GetForeignKeyMembers()'访问安全关键类型'System.ComponentModel.DataAnnotations.AssociationAttribute'失败。程序集'System.ComponentModel.DataAnnotations,Version=4.0.0.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35'是有条件的APTCA程序集,在当前AppDomain中未启用。请在创建AppDomain时将程序集名称'System.ComponentModel.DataAnnotations,PublicKey=0024000004800000940000000602000000240000525341310004000001000100B5FC90E7027F67871E773A8FDE8938C81DD402BA65B9201D60593E96C492651E889CC13F1415EBB53FAC1131AE0BD333C5EE6021672D9718EA31A8AEBD0DA0072F25D87DBA6FC90FFD598ED4DA35E44C398C454307E8E33B8426143DAEC9F596836F97C8F74750E5975C64E2189F45DEF46B2A2B1247ADC3652BF5C308055DA9'添加到PartialTrustVisibleAssemblies列表中,以使该程序集可由部分信任或安全透明代码使用。

readHelper.ThreadStart() 内部异常:


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

我将我的应用程序更改为在完全信任下运行,但没有成功 :(

有什么想法吗?


如果您正在调试模式下运行,请确保转到项目属性->调试->取消选中“启用Visual Studio托管进程”,然后您将停止收到此错误。 - Jonathan DeMarks
2个回答

1
不仅是可能的,而且这里有完整的代码清单,提供了可由 Excel PowerPivot 消耗的 OData 的 RIA。请记住,您必须关闭 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();
              }
       }
}

0

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

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

另外请检查你的exe文件的*.config文件,我记得有一些与IIS相关的设置需要删除。

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接