使用单个合同运行ServiceHost工作正常,如下所示:
servicehost = new ServiceHost(typeof(MyService1));
servicehost.AddServiceEndpoint(typeof(IMyService1), new NetTcpBinding(), "net.tcp://127.0.0.1:800/MyApp/MyService1");
servicehost.Open();
Run Code Online (Sandbox Code Playgroud)
现在我想添加第二个(第3个,第4个......)合同.我的第一个猜测就是添加更多端点,如下所示:
servicehost = new ServiceHost(typeof(MyService1));
servicehost.AddServiceEndpoint(typeof(IMyService1), new NetTcpBinding(), "net.tcp://127.0.0.1:800/MyApp/MyService1");
servicehost.AddServiceEndpoint(typeof(IMyService2), new NetTcpBinding(), "net.tcp://127.0.0.1:800/MyApp/MyService2");
servicehost.Open();
Run Code Online (Sandbox Code Playgroud)
但当然这不起作用,因为在ServiceHost的创建中我可以将MyService1作为参数或MyService2传递 - 所以我可以为我的服务添加很多端点,但所有必须使用相同的合同,因为我只能提供一个实现?
我觉得我在这里错过了重点.当然必须有一些方法来为我添加的每个端点合同提供实现吗?
我正在尝试在web api自托管主机中获取用户代理,我要么做错了,要么web api本身正在改变用户代理字符串.
我尝试使用几种方法来获取字符串,它们都返回相同的结果,而不是例外的"Mozilla/5.0(Windows NT 6.2; WOW64)AppleWebKit/537.31(KHTML,像Gecko)Chrome/26.0.1410.28 Safari /537.31",我只得到"Mozilla/5.0".
我试过了:
var header = request.Headers.SingleOrDefault(h => h.Key == "User-Agent").Value.First();
var header = request.Headers.UserAgent.SingleOrDefault().Product.ToString();
var header = request.Headers.GetValues("User-Agent").FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)
我做错了,它是自我主持人所以我没有上下文可以使用.
是否有可能在另一个应用程序中自我托管asp.net mvc,即.控制台,窗体,服务等
我想构建一个提供Web界面来控制它的应用程序,我想使用asp.net mvc作为它的Web部分.
我确实快速看了一下看起来会起作用的Nancy,虽然它不支持asp.net mvc它确实支持razor,尽管它与asp.net没有完全相同的支持(例如强类型视图) )
我也找到了这个问题,但它没有真正深入 可能使用没有IIS的ASPNET MVC2?
我想在Localhost上安装我的WordPress博客,推入GitHub并在GitHub上作为静态页面运行.我可以这样做,如果是,请详细解答所涉及的步骤和问题?
我不在乎我的页面是静态的,但是我可以在GitHub页面上托管它吗?
在ASP.NET Web API中,HttpControllerContext实例提供了有关当前环境的大量信息,包括当前请求的URI.
如果服务依赖于此类信息(例如请求URI),则应该可以将该信息注入服务中.
使用Poor Man的DI很容易做到:只需实现一个自定义的IHttpControllerActivator.
然而,随着温莎城堡,这突然变得非常困难.以前,我已经描述了解决这个问题的一种非常复杂的方法,但它取决于PerWebRequest的生活方式,并且事实证明这种生活方式在自托管方案中不起作用,因为HttpContext.Current是空的.
到目前为止,我已经能够通过从自定义IHttpControllerActivator将所需信息作为内联参数传递给Resolve方法来完成这项工作:
public IHttpController Create(
HttpControllerContext controllerContext,
Type controllerType)
{
var baseUri = new Uri(
controllerContext
.Request
.RequestUri
.GetLeftPart(UriPartial.Authority));
return (IHttpController)this.container.Resolve(
controllerType,
new { baseUri = baseUri });
}
Run Code Online (Sandbox Code Playgroud)
但是,默认情况下,仅当立即请求的类型依赖于参数时才会起作用(即,如果请求的Controller本身依赖于该参数baseUri).如果依赖关系baseUri更深层地隐藏在依赖关系层次结构中,则默认情况下它不起作用,因为内联参数不会传播到更深层.
可以使用自定义IDependencyResolver(Castle Windsor IDependencyResolver,而不是ASP.NET Web API IDependencyResolver)更改此行为:
public class InlineDependenciesPropagatingDependencyResolver :
DefaultDependencyResolver
{
protected override CreationContext RebuildContextForParameter(
CreationContext current, Type parameterType)
{
if (parameterType.ContainsGenericParameters)
{
return current;
}
return new CreationContext(parameterType, current, true); …Run Code Online (Sandbox Code Playgroud) dependency-injection castle-windsor self-hosting asp.net-web-api
如何在所有网络接口上进行Web API自主绑定?
我目前有以下代码.不幸的是,它仅绑定在localhost上.因此,从localhost以外的地方访问此服务器失败.
var baseAddress = string.Format("http://localhost:9000/");
using (WebApp.Start<Startup> (baseAddress))
{
Console.WriteLine("Server started");
Thread.Sleep(1000000);
}
Run Code Online (Sandbox Code Playgroud) 我创建了我的第一个自托管WCF服务.我在C#控制台应用程序中托管它,但它抛出一个错误:
System.ServiceModel.AddressAccessDeniedException:HTTP无法注册URL http:8080
当我以管理员身份运行Visual Studio 2013时,它运行良好,但如果不运行则不行.那么有什么方法可以自动完成它而不是将VS作为ADMIN启动?
到目前为止,我创建了一个HelloService类库,在其中我添加了一个WCF服务,该服务由一个接口IHelloService和HelloService.
IHelloService:
namespace HelloService
{
[ServiceContract]
public interface IHelloService
{
[OperationContract]
String GetMsg();
}
}
Run Code Online (Sandbox Code Playgroud)
HelloService:
namespace HelloService
{
public class HelloService : IHelloService
{
public String GetMsg()
{
return "Service Accessed";
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后我创建了一个C#控制台应用程序HelloServiceHost,它有一个app.config文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors >
<behavior name="MexBehaviour">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="HelloService.HelloService"
behaviorConfiguration="MexBehaviour" >
<endpoint
address="HelloService"
binding="basicHttpBinding"
contract="HelloService.IHelloService"></endpoint>
<endpoint …Run Code Online (Sandbox Code Playgroud) 我有一个简单的.net核心web api,只有一个动作:
[Route("[action]")]
public class APIController : Controller
{
// GET api/values
[HttpGet]
public string Ping()
{
return DateTime.Now.ToString();
}
}
Run Code Online (Sandbox Code Playgroud)
如果我通过dotnet运行运行,我得到
Hosting environment: Production
Content root path: C:\Users\xxx\Documents\Visual Studio 2015\Projects\SelfHostTest\src\SelfHostTest
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.
Run Code Online (Sandbox Code Playgroud)
转到浏览器并输入http:// localhost:5000/ping会导致成功返回当前时间.但是,转到远程计算机(相同的LAN)并尝试通过http:// odin:5000/ping访问服务会导致404错误.(Odin是通过dotnet运行在控制台中运行web api的机器的名称).
服务器(和客户端!)防火墙都已关闭.我可以成功地ping"odin".
任何想法我在这里缺少什么简单的步骤.我在家里和工作中尝试过这种方法并没有成功.
我试图使用ASP.NET Web API自我主机选项与Windows身份验证,以便我可以确定登录用户,并最终根据用户的身份接受或拒绝用户.这是我的控制台应用程序代码:
using System;
using System.Web.Http;
using System.Web.Http.SelfHost;
namespace SelfHost
{
class Program
{
static void Main(string[] args)
{
var config = new HttpSelfHostConfiguration("http://myComputerName:8080");
config.UseWindowsAuthentication = true;
config.Routes.MapHttpRoute(
"API Default", "api/{controller}/{id}",
new { id = RouteParameter.Optional });
using (HttpSelfHostServer server = new HttpSelfHostServer(config))
{
server.OpenAsync().Wait();
Console.WriteLine("Press Enter to quit.");
Console.ReadLine();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是控制器:
[Authorize]
public class HelloController : ApiController
{
public string Get()
{
// This next line throws an null reference exception if the Authorize
// …Run Code Online (Sandbox Code Playgroud) asp.net asp.net-mvc windows-authentication self-hosting asp.net-web-api
我已经创建了一个自托管的Nancy/SignalR应用程序,它使用Microsoft.Owin.Host.HttpListener和在OWIN中自托管Microsoft.Owin.Hosting
事情在本地工作得很好但是一旦我尝试使用除localhost之外的任何东西来访问应用程序我就会收到HTTP Error 503. The service is unavailable错误.我甚至无法使用127.0.0.1或使用机器名称访问应用程序.
我已经尝试将端口添加到urlacl使用
http add urlacl http://*:8989/ user=EVERYONE 但似乎没有做任何事情.
这是我试过的OWIN启动选项,
var options = new StartOptions
{
Url = "127.0.0.1",
App = GetType().AssemblyQualifiedName,
Port = _configFileProvider.Port
};
var options = new StartOptions
{
App = GetType().AssemblyQualifiedName,
Port = _configFileProvider.Port
};
Run Code Online (Sandbox Code Playgroud)
这是启动和停止服务器的文件的源代码 https://github.com/NzbDrone/NzbDrone/blob/vnext/NzbDrone/Owin/OwinHostController.cs
self-hosting ×10
asp.net ×3
c# ×3
asp.net-mvc ×2
wcf ×2
.net-core ×1
acl ×1
github ×1
github-pages ×1
nancy ×1
owin ×1
signalr ×1
wcf-binding ×1
web-hosting ×1
wordpress ×1