ServiceStack中的AmbiguousMatchException异常?

时间:2015-10-14 12:21:37

标签: servicestack

PFB我的代码。

namespace ManualCSharpe
{
    public class MyServices : Service
    {
        [Route("/L/hello/")] //RequestDTO one
        public class HelloL
        {
            public string Name { get; set; }
        }
        [Route("/H/hello/")] //RequestDTO two
        public class HelloH
        {
            public string Name1 { get; set; }
        }
        public class HelloResponse //ResponseDTO
        {
            public string Result { get; set; }
        }

        public class HelloServiceL : Service //Service One
        {
            public object Get(HelloL request)
            {
                return new HelloResponse { Result = "Low" };
            }
         }
        public class HelloServiceH : Service //Service 
        {
            public object Get(HelloH request)
            {
                return new HelloResponse { Result = "High" };
            }
        }

        //Define the Web Services AppHost
        public class AppHost : AppSelfHostBase
        {
            public AppHost()
                : base("HttpListener Self-Host",new Assembly[] {typeof(HelloServiceL).Assembly, typeof(HelloServiceH).Assembly}) { }


            public override void Configure(Funq.Container container) { }
        }

        //Run it!
        static void Main(string[] args)
        {
            var listeningOn = args.Length == 0 ? "http://*:133/" : args[0];
            var appHost = new AppHost()
                .Init()
                .Start(listeningOn);

            Console.WriteLine("AppHost Created at {0}, listening on {1}",
                DateTime.Now, listeningOn);

            Console.ReadKey();
        }
    }
}

当我要添加两个服务时,它会显示在异常下面。

An unhandled exception of type 'System.Reflection.AmbiguousMatchException' occurred in ServiceStack.dll

Additional information: Could not register Request 'ManualCSharpe.MyServices+HelloL' with service 'ManualCSharpe.MyServices+HelloServiceL' as it has already been assigned to another service.

Each Request DTO can only be handled by 1 service.

我有以下douts。

  1. 这里我创建了两个不同的DTO for Two Service。为何显示错误Each Request DTO can only be handled by 1 service.简单来说,Two routetwo DTO with two Service映射。
  2. 我可以为具有多项服务的多个RequestDTO创建一条路由吗?简单来说,可以使用两个DTO /L/hello/HelloL映射一条路线HelloH

3 个答案:

答案 0 :(得分:3)

您不能将Service类实现嵌套在另一个外部MyServices类中:

public class MyServices : Service
{
    [Route("/L/hello/")] //RequestDTO one
    public class HelloL
    {
        public string Name { get; set; }
    }
    [Route("/H/hello/")] //RequestDTO two
    public class HelloH
    {
        public string Name1 { get; set; }
    }
    public class HelloResponse //ResponseDTO
    {
        public string Result { get; set; }
    }

    public class HelloServiceL : Service //Service One
    {
        public object Get(HelloL request)
        {
            return new HelloResponse { Result = "Low" };
        }
     }
    public class HelloServiceH : Service //Service 
    {
        public object Get(HelloH request)
        {
            return new HelloResponse { Result = "High" };
        }
    }
}

完全删除外部MyServices类,直接将DTO和Service类放在C#名称空间下。

此外,路由不应以/后缀结尾,因此我要更改:

[Route("/L/hello/")]

为:

[Route("/L/hello")]

答案 1 :(得分:2)

@mythz答案对于OP是正确的但是我来到这里寻找一个不同情况的答案,原因并不是特别明显 - 如果你试图两次注册同一个程序集,你会得到这个例外,例如,如果你将服务实现移动到同一个程序集中并将其拉入:

 public AppHost() : base("App", typeof(AdminService).GetAssembly(), typeof(InboundService).GetAssembly(),typeof(ProductService).GetAssembly()) 

答案 2 :(得分:0)

对于那些通过谷歌搜索来到这里的人来说,ServiceStack中的AmbiguousMatchException例外有时可以在ServiceStack中触发,但在内部处理。

您可以更改例外设置,使其不会因此异常而中断。

我已经更改了我的异常设置以打破所有异常,这让我陷入了一段时间。