我是第一次构建服务堆栈:hello world.
但它给了我一个错误:找不到请求处理程序:可能缺少的部分是什么?谢谢.
这是我的global.asax.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using ServiceStack.ServiceHost;
using ServiceStack.WebHost.Endpoints;
namespace ServiceStack.SearchService
{
public class Global : System.Web.HttpApplication
{
public class Hello { public string Name { get; set; } }
public class HelloResponse { public string Result { get; set; } }
public class HelloService : IService<Hello>
{
public object Execute(Hello request)
{
return new HelloResponse { Result = "Hello, " + request.Name }; …Run Code Online (Sandbox Code Playgroud) 我正在用F#编写ServiceStack Web服务,并且需要限制一些功能(例如删除SOAP支持).
在C#中,我使用管道操作将多个枚举(ServiceStack.ServiceHost.Feature)分配给EnableFeatures属性,如下所示:
SetConfig(new EndpointHostConfig
{
DebugMode = true, //Show StackTraces in responses in development
EnableFeatures = Feature.Json | Feature.Xml | Feature.Html | Feature.Metadata | Feature.Jsv
});
Run Code Online (Sandbox Code Playgroud)
但是在F#中你不能使用管道来完成这个,我尝试的其他一切都试图对枚举进行功能应用.在这种情况下,如何分配多个枚举?
我试图围绕ServiceStack,虽然它声称它有非常好的文档,但到目前为止似乎并非如此.是否有文档实际上说明了要使用哪些接口/基类,以及它们做了什么?
只是..有一堆问题,并且可以找到很少的答案..一个新的API设计显示了一个DTO实现IReturn接口的例子,以及从服务继承的服务 - 但是如果这是现在的首选方式则没有解释,是吗需要实现IReturn,如何处理POST/GET /等等等.
任何链接将不胜感激.
是的,我有样品,但是例如他们在这个IReturn接口上没有任何东西..并且样品无论如何都不会打败文档.
基本的连线看起来很简单,但是我很难理解如何配置NLog.鉴于以下设置,如何设置配置以将文本文件转储到文件夹?
APPHOST:
LogManager.LogFactory = new NLogFactory();
Run Code Online (Sandbox Code Playgroud)
在App Logic中:
ILog log = LogManager.GetLogger(GetType());
log.InfoFormat("Something happened");
Run Code Online (Sandbox Code Playgroud)
一个Config文件,如:
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<targets>
<target name="console" xsi:type="ColoredConsole"
layout="${date:format=HH\:mm\:ss}|${level}|${stacktrace}|${message}" />
<target name="file" xsi:type="File" fileName="${specialfolder:folder=ApplicationData}/logs/App.log"
layout="${date}: ${message}" />
<target name="eventlog" xsi:type="EventLog" source="My App" log="Application"
layout="${date}: ${message} ${stacktrace}" />
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="file" />
<logger name="*" minlevel="Fatal" writeTo="eventlog" />
</rules>
Run Code Online (Sandbox Code Playgroud)
ServiceStack中的身份验证,存储库和缓存提供程序提供了一种向Web应用程序添加登录会话的简单方法,几乎不需要其他代码.我发现可以配置身份验证提供程序的会话超时,例如:
new CredentialsAuthProvider { SessionExpiry = TimeSpan.FromMinutes(10) }
Run Code Online (Sandbox Code Playgroud)
这提供了从登录点到期的到期日.如果我们正在开发一个必须在短时间内将用户注销的安全系统,那么我们会将其从默认的2周更改为类似上面的示例.但是这有一个问题,即无论用户是否仍在与应用程序进行交互,登录用户的10分钟将被踢出.
是否有一种简单的方法可以告诉会话提供者延长调用服务时的到期时间?
理想情况下,它允许我们扩展特定服务/请求的会话(以便仅在用户主动与应用程序交互时扩展会话,因此可以忽略轮询的服务).
更新
基于mythz给出的答案,我们现在有一个简单的解决方案,通过扩展ResponseFilterAttribute来提供我们所需的控制级别.
是否可以返回自定义身份验证响应?我已经拥有自己的自定义身份验证提供程序,它继承自CredentialsAuthProvider.
我想在响应中返回会话到期日期,以便客户端确切地知道他们的服务器会话何时到期:
{
"sessionId": "bG27SdxbRkqJqU6xv/gvBw==",
"userName": "joe.bloggs@letmein.com",
"sessionExpires": "2013-04-29T03:27:14.0000000",
"responseStatus": {}
}
Run Code Online (Sandbox Code Playgroud)
我可以像这样覆盖Authenticate方法:
public override object Authenticate(IServiceBase authService, IAuthSession session, Auth request)
{
// get base response
var response = base.Authenticate(authService, session, request);
// grab the session
var customSession = authService.GetSession() as CustomUserSession;
// if response can be cast and customSession exists
if (response is AuthResponse && customSession != null)
{
// cast
var authResponse = response as AuthResponse;
// build custom response
var customAuthResponse = new CustomAuthResponse
{
ReferrerUrl …Run Code Online (Sandbox Code Playgroud) 我希望能够发布一个文件,并作为该帖子的一部分添加数据.
这是我有的:
var restRequest = new RestRequest(Method.POST);
restRequest.Resource = "some-resource";
restRequest.RequestFormat = DataFormat.Json;
string request = JsonConvert.SerializeObject(model);
restRequest.AddParameter("text/json", request, ParameterType.RequestBody);
var fileModel = model as IHaveFileUrl;
var bytes = File.ReadAllBytes(fileModel.LocalStoreUrl);
restRequest.AddFile("FileData", bytes, "file.zip", "application/zip");
var async = RestClient.ExecuteAsync(restRequest, response =>
{
if (PostComplete != null)
PostComplete.Invoke(
new Object(),
new GotResponseEventArgs
<T>(response));
});
Run Code Online (Sandbox Code Playgroud)
它发布文件很好,但数据不存在 - 这甚至可能吗?
[UPDATE]
我修改了代码以使用多部分标题:
var restRequest = new RestRequest(Method.POST);
Type t = GetType();
Type g = t.GetGenericArguments()[0];
restRequest.Resource = string.Format("/{0}", g.Name);
restRequest.RequestFormat = DataFormat.Json;
restRequest.AddHeader("content-type", "multipart/form-data");
string …Run Code Online (Sandbox Code Playgroud) 有没有人有成功(生产代码)在Mono上托管独立的异步Web API(asp.net web API)服务?独立我的意思是在asp.net之外的控制台应用程序中托管API.
我正在寻找一种简单的方法来创建REST API,我真的很想让我的堆栈异步(C#5样式)从顶层HTTP层到底层数据访问层,现在C#5有这么好的支持为了它.
通常我会使用ServiceStack并将其作为Linux上的守护进程托管,但由于ServiceStack不支持其服务中的新C#5异步内容(据我所知),我正在考虑使用自托管的异步Web API在Mono上.
我知道,有在ServiceStack的方式有些异步分支,但还没有准备好,我知道有在ServiceStack一些asynconeway东西,但我不认为这是使用新的基于任务异步的东西在C# 5.
所以我的问题是,在单声道上使用自托管异步Web API制作REST服务是否可行且稳定,或者在Mono上进行独立托管时是否更好地使用同步ServiceStack?
我们有一个使用ServiceStack连接的ASP.NET Web应用程序.我之前从未编写过功能测试,但我们的任务是针对我们的API编写测试(nUnit)并证明它一直工作到数据库级别.
有人可以帮我开始编写这些测试吗?
以下是post我们的用户服务的方法示例.
public object Post( UserRequest request )
{
var response = new UserResponse { User = _userService.Save( request ) };
return new HttpResult( response )
{
StatusCode = HttpStatusCode.Created,
Headers = { { HttpHeaders.Location, base.Request.AbsoluteUri.CombineWith( response.User.Id.ToString () ) } }
};
}
Run Code Online (Sandbox Code Playgroud)
现在我知道如何编写一个标准的单元测试,但我对此部分感到困惑.我是否必须通过HTTP调用WebAPI并初始化Post?我是否只是像单位测试那样调用方法?我想这是"功能测试"的一部分让我望而却步.
我有一个JSON字符串,看起来像:
"{\"Id\":\"fb1d17c7298c448cb7b91ab7041e9ff6\",\"Name\":\"John\",\"DateOfBirth\":\"\\/Date(317433600000-0000)\\/\"}"
Run Code Online (Sandbox Code Playgroud)
我正在尝试将其反序列化为object(我正在实现一个缓存接口)
我遇到的麻烦就是我用的时候
JsonSerializer.DeserializeFromString<object>(jsonString);
Run Code Online (Sandbox Code Playgroud)
它回来了
"{ID:6ed7a388b1ac4b528f565f4edf09ba2a,名称:约翰,出生日期:/日期(317433600000-0000)/}"
是对的吗?
我无法断言任何事情......我也不能使用动态关键字....
有没有办法从ServiceStack.Text库返回一个匿名对象?
c# anonymous-types servicestack json-deserialization servicestack-text
servicestack ×10
c# ×4
api ×1
asp.net ×1
async-await ×1
enums ×1
f# ×1
mono ×1
nlog ×1
nunit ×1
rest ×1
restsharp ×1
web-services ×1