标签: servicestack

ServiceStack拦截请求/响应以进行验证

我试图拦截ServiceRunner中的请求和响应以对它们运行验证.

1.我不知道如何(!result.IsValid)中止请求,或者这是否是正确的方法.

2.特别是对于响应,对象响应是未知的,直到动态运行时,这使得我很难为它创建IValidator,它在编译时需要已知类型.

请参阅代码中的注释:

public class CustomServiceRunner<T> : ServiceRunner<T> {

    public CustomServiceRunner(IAppHost appHost, ActionContext actionContext) 
        : base(appHost, actionContext) { }

    public override void BeforeEachRequest(IRequestContext requestContext, T request) {
        var validator = AppHost.TryResolve<IValidator<T>>();
        var result = validator.Validate(request);
        //----------------------
        //if (!result.IsValid) 
        //  How to return result.ToResponseDto() and abort the request?
        //----------------------
        base.BeforeEachRequest(requestContext, request);
    }
    public override object AfterEachRequest(IRequestContext requestContext, T request, object response) {
        //-----------------------
        //Validating against response presents a more challenging issue
        //I may have multiple response classes and returned response …
Run Code Online (Sandbox Code Playgroud)

c# servicestack

1
推荐指数
1
解决办法
3745
查看次数

Servicestack - 无法从C#Client读取或设置cookie

我遇到ServiceStack和cookie的问题 - 我无法从内置的C#JsonServiceClient中读取或设置我的服务上的cookie.

Web服务正在SSL上运行在此域上:

https://sites.sub.domain.co.uk/Api/
Run Code Online (Sandbox Code Playgroud)

并且调用服务的网站位于:

https://sites.sub.domain.co.uk/Website/
Run Code Online (Sandbox Code Playgroud)

当我尝试在服务中设置cookie时,因此:

this.Response.Cookies.AddCookie(cookie);
Run Code Online (Sandbox Code Playgroud)

在哪里cookie设置域.domain.co.uk,没有任何反应.该服务完全按预期执行并返回预期的响应,但cookie未设置,并且没有异常.

我还尝试使用提供的res对象在响应过滤器中执行此操作,结果相同.

其次,当使用ac#client调用服务时,似乎没有cookie被发送.我正在AppHost的每个请求中注销整个请求,

RequestFilters.Add((req, res, dto) => { Log(req.Cookies); });
Run Code Online (Sandbox Code Playgroud)

并且cookie总是空白的,虽然我可以在浏览器中清楚地看到存在多个cookie,并且fiddler Cookie在请求中显示了满满的东西.

正在进行的调用是一个非常糟糕的标准JsonServiceClient POST请求,由aspx页面上的代码构成:

var client = new JsonServiceClient("somewhere");

var response = client.Post(new RequestForStuff());
Run Code Online (Sandbox Code Playgroud)

任何建议最受欢迎.

更新

在调查时,我通过html表单发布了一个服务,并且在我记录请求时确实有cookie集合...我希望看到的所有cookie都在那里......

另一个更新

我继续用不同的方法测试服务.在Chrome中使用REST控制台时,浏览器会发送cookie,而我正在尝试在响应上创建的cookie都正常工作,但同样,当使用相同的参数调用相同的服务时,来自ac# JsonServiceClient,既不发送也不设置cookie.

来自成功请求的头文件(来自REST控制台)(在日志文件中序列化为json):

{
  "Header": "Connection",
  "Value": "keep-alive"
},
{
  "Header": "Content-Length",
  "Value": "124"
},
{
  "Header": "Content-Type",
  "Value": "application/json"
},
{
  "Header": "Accept",
  "Value": "application/json"
},
{
  "Header": "Accept-Charset",
  "Value": "ISO-8859-1,utf-8;q=0.7,*;q=0.3"
},
{ …
Run Code Online (Sandbox Code Playgroud)

servicestack

1
推荐指数
1
解决办法
1690
查看次数

ServiceStack ResolveService

所以我的问题围绕调用以下url中描述的apphost.ResolveService: 从Razor调用ServiceStack服务

我在_Layout.cshtml中

显然下面的代码工作得很好,但正如上面的url中的答案所示,它有点愚蠢

SMSGateway.Services.EntityCollectionResponse response = 
    new ServiceStack.ServiceClient.Web.JsonServiceClient(
        "http://localhost:1337/")
        .Get<SMSGateway.Services.EntityCollectionResponse>(
            "/entities");
Run Code Online (Sandbox Code Playgroud)

所以这给了我一个实体列表:)但不是最优的...所以这是我尝试以正确的方式做到这一点

var response = ConsoleAppHost.Instance
    .ResolveService<SMSGateway.Services.EntityService>(
        HttpContext.Current).Get(
            new SMSGateway.Services.EntitiesRequest());

// SMSGateway.Services.EntityCollectionResponse response =
//     base.Get<SMSGateway.Services.EntityService>().Get(
//         new SMSGateway.Services.EntitiesRequest());

foreach (var entity in response.Result)
{
    <li>
        <a href="@entity.MetaLink.Href">
            @Html.TitleCase(entity.Name) entities
        </a>
    </li>
}
Run Code Online (Sandbox Code Playgroud)

好的,我得到的错误如下:

错误CS0122:由于其保护级别,ConsoleAppHost无法访问....

这是预期的吗?我在考虑是否这不是我可能不允许在_Layout.cshtml文件中调用它的情况?

进一步阅读带我进入了.NET 2.0中的InternalVisibleTo Testing Internal Methods文章

我觉得非常有趣:P但没有雪茄:)

inversion-of-control dto razor servicestack

1
推荐指数
1
解决办法
368
查看次数

使用ServiceStack.Logging.NLog而不是直接NLog调用有什么缺点?

我喜欢NLOG由于描述的多种原因  https://robertmccarter.com/switching-to-nlog  和

log4net与Nlog

但是我考虑   https://github.com/ServiceStack/ServiceStack  和 https://nuget.org/packages/ServiceStack.Logging.NLog以保持通用接口和将来切换到其他提供商的能力.

是否有人知道使用ServiceStack.Logging.NLog而不是直接NLog调用的任何缺点/限制?

它会导致任何重要的性能下降吗?

直接调用NLog记录器时是否可以使用任何功能,但ServiceStack不支持?

logging nlog servicestack

1
推荐指数
1
解决办法
985
查看次数

JsonServiceClient似乎不包含在程序集中

在继续学习和使用ServiceStack时,我正在尝试使用ac#/ WPF应用程序来使用hello服务.

我已经完成了使用NuGet安装所需文件的预期步骤:

PM> install-package servicestack.common
Run Code Online (Sandbox Code Playgroud)

我相信我已经导入了正确的命名空间:

using ServiceStack.Common;
using ServiceStack.Common.ServiceClient.Web;
Run Code Online (Sandbox Code Playgroud)

然而,当我尝试按照我在Stack和Github上找到的示例时,VS10报告无法找到类型或命名空间.

var client = new JsonServiceClient("http://172.16.0.15/");
Run Code Online (Sandbox Code Playgroud)

我也无法使用我认为完全限定的名称创建此对象:

var client = new ServiceStack.ServiceClient.web.JsonServiceClient . . . 
Run Code Online (Sandbox Code Playgroud)

是否有必须安装的另一个包或必须进行的另一个引用才能使用此类?

更新:

达林建议的完全限定类型似乎没有办法:

var client = new ServiceStack.ServiceClient.Web.JsonServiceClient("http://172.16.0.15/");
Run Code Online (Sandbox Code Playgroud)

我仍然得到VS10报告:

"The type or namespace name 'ServiceClient' does not exist in the namespace 'ServiceStack'.... "
Run Code Online (Sandbox Code Playgroud)

c# wpf nuget servicestack

1
推荐指数
1
解决办法
1734
查看次数

找不到ServiceStack RequestContext

您好我刚刚在VS中创建了一个新的Empty webApp并安装了servicestack Nugets.

我一直在寻找缓存响应内存(通过MemCached),但在服务Any方法中我无法访问该base.RequestContext对象.

类DO继承自ServiceStack.Service(ServiceStack.ServiceInterface.Service之前不是吗?).

    public class UtentiService : Service
{

    public object Get(I_Utenti request) 
    {
        var cacheKey = "myKey";

        return  base.RequestContext.ToOptimizedResultUsingCache(base.Cache, cacheKey, () =>{});
    }
}
Run Code Online (Sandbox Code Playgroud)

但我无法访问base.RequestContext.为什么?

HSH,披萨!

c# memcached servicestack

1
推荐指数
1
解决办法
1325
查看次数

如何在ServiceStack路由中使用斜杠传递参数?

我的要求是在ServiceStack路由中传递一个值作为参数,该路由包含类似" SK-LOT-79-14/3/11 "的斜杠,这样我就可以获取服务中的记录.

示例路由配置:

[Route("/cims/qcHistoryByLot/{lotNumber}", "GET")]
Run Code Online (Sandbox Code Playgroud)

示例批号:SK-LOT-79-14/3/11

c# servicestack

1
推荐指数
1
解决办法
506
查看次数

ServiceStack:更改自托管应用程序中所有路由的基本路径

我有一个自托管应用程序,其中设置了许多路由.而不是通过每一个去和改变路线是/api/<route>其中<route>在现有的路线,我在想,如果我可以用前缀每条路线/api的时候,我开始申请?我知道它可以在IIS托管的环境中设置它web.config但我不确定它是否可能在自托管环境中?

servicestack servicestack-bsd

1
推荐指数
1
解决办法
613
查看次数

ServiceStack的Funq和ElasticSearch

我正在尝试使用ServiceStack的Funq连接ElasticClient,但是在尝试调用它时我得到一个空引用异常.

这是我的设置:

在AppHost.cs中:

    var elasticSettings = new ConnectionSettings(new Uri("http://localhost:9200"), "listings");
    var elasticClient = new ElasticClient(elasticSettings);
    container.Register(elasticClient);
Run Code Online (Sandbox Code Playgroud)

然后在我的ServiceInterface项目中,在ListingServices.cs类中:

       private ElasticClient Elastic; // also tried IElasticClient Elastic;


       public object Post(CreateListing request)
        {
            Listing newAd = new Listing();
            newAd = request.ConvertTo<Listing>();
            using (IDbConnection db = DbFactory.Open())
            {

                db.Save(newAd);
                Elastic.Index(newAd); // NULL REFERENCE EXCEPTION
            }
            return new CreateListingResponse { Result = true };
        }
Run Code Online (Sandbox Code Playgroud)

但是,Elastic仍设置为Null并提供空引用异常.

关于如何解决的任何想法.

elasticsearch servicestack funq

1
推荐指数
1
解决办法
238
查看次数

ServiceStack Javascript / Typescript客户端和CORS

根据文档,我相信这是启用CORS的唯一要求:

Plugins.Add(new CorsFeature());

然后从另一个网站:

var client = new JsonServiceClient('https://my-app.azurewebsites.net');
client.get(new something());
Run Code Online (Sandbox Code Playgroud)

返回的错误是:

对预检请求的响应未通过访问控制检查:响应中的“ Access-Control-Allow-Credentials”标头的值为“”,当请求的凭据模式为“ include”时,该值必须为“ true”。

没有身份验证,我使用的是所有默认值,JsonServiceClient设置中的此处缺少什么以调用其他服务器?

cors servicestack

1
推荐指数
1
解决办法
402
查看次数