public class ContactsController : ApiController
{
private static readonly List<Contact> _contacts = new List<Contact>();
public Contact PutContacts(int id, Contact contact)
{
if (_contacts.Any(c => c.Id == contact.Id) == false)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
contact.LastModified = DateTime.Now;
return contact;
}
}
Run Code Online (Sandbox Code Playgroud)
http put header:
PUT /api/contacts/3 HTTP/1.1
User-Agent: Fiddler
Content-Type: application/json
Host: localhost:8080
Run Code Online (Sandbox Code Playgroud)
身体:
{"Id":3,"Name":"mmm","Phone":"000 000 0000","Email":"mmm@gmail.com","LastModified":"2012-03-08T23:42:13.8681395+08:00"}
Run Code Online (Sandbox Code Playgroud)
响应:
HTTP/1.1 400 Bad Request
"The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'SelfHost.Contact PutContacts(Int32, …Run Code Online (Sandbox Code Playgroud) 我在app.config中有这个配置:
</binding>
</basicHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="myBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
Run Code Online (Sandbox Code Playgroud)
我想以编程方式从我的桌面应用程序公开此服务:
我定义了主机实例:
ServiceHost host = new ServiceHost(typeof(MyType), new Uri("http://" + hostName + ":" + port + "/MyName"));
Run Code Online (Sandbox Code Playgroud)
然后我用它的绑定添加端点:
var binding = new BasicHttpBinding("myBinding");
host.AddServiceEndpoint(typeof(IMyInterface), binding, "MyName");
Run Code Online (Sandbox Code Playgroud)
现在,我想用一些从配置文件中读取名为myBehavior的行为的代码替换下面的代码,而不是对行为选项进行硬编码.
ServiceMetadataBehavior smb = new ServiceMetadataBehavior() { HttpGetEnabled = true };
host.Description.Behaviors.Add(smb);
// Enable exeption details
ServiceDebugBehavior sdb = host.Description.Behaviors.Find<ServiceDebugBehavior>();
sdb.IncludeExceptionDetailInFaults = true;
Run Code Online (Sandbox Code Playgroud)
然后,我可以打开主机.
host.Open();
Run Code Online (Sandbox Code Playgroud)
*编辑*
使用配置文件配置服务
您不应该需要这种方式,您应该让主机从配置文件中自动获取其配置,而不是手动提供它们,阅读本文(使用配置文件配置服务),它将帮助您,我已经托管了我的服务C#中的单行和config中的少行.
这是关于(在代码中配置WCF服务)的第二篇文章,我的错是我试图混合两种方式!
我将添加此作为答案.
我试图在控制台应用程序中运行Nancy中的SignalR.
当我的浏览器$.connection.hub.start()它得到404 - NotFound的//localhost:667/negotiate?clientProtocol=1.3
---- 8 <----
我(尝试)在一个端口上运行Nancy而在另一个端口上运行SignalR.南希与剃刀合作.SignalR返回集线器javascript.
(对不起下面的代码量,但是我无法进一步减少它.)
(这个问题可能会从之前已被删除的问题中得到认可 - 我已经标记错误.)
客户代码:
<script type="text/javascript" src='/Scripts/jquery-1.6.4.min.js'></script>
<script type="text/javascript" src="/Scripts/jquery.signalR-2.0.0-beta2.js"></script>
<script src="http://localhost:667/signalr/hubs" type="text/javascript"></script>
var chat;
$(function () {
$.connection.hub.url = '//localhost:667';
$.connection.hub.logging = true;
chat = $.connection.chat;
chat.client.addMessage = onAddMessage; // declared but not here
$.connection.hub.start()
.done(function () {
alert($.connection.id);
chat.server.send('Works!');
})
.fail(function ( failreason ) {
alert( failreason );
});
});
Run Code Online (Sandbox Code Playgroud)
服务器代码(在作为admin运行的控制台程序中)
class Program
{
static void Main(string[] args)
{
const string webUrl = "http://localhost:666"; …Run Code Online (Sandbox Code Playgroud) 自托管不支持HttpContext.
当我运行自我托管的内存中集成测试时,这段代码也不起作用:
// OWIN Self host
var owinEnvProperties = request.Properties["MS_OwinEnvironment"] as IDictionary<string, object>;
if (owinEnvProperties != null)
{
return owinEnvProperties["server.RemoteIpAddress"].ToString();
}
Run Code Online (Sandbox Code Playgroud)
owinEnvProperties始终为null.
那么我应该如何使用自托管来获取客户端IP地址?
namespace helloserviceSelfHostingDemo
{
[ServiceContract]
interface IhelloService
{
[OperationContract]
string sayhello(string name);
}
public class HelloService : IhelloService
{
public string sayhello(string name)
{
return "hello " + name;
}
}
class Program
{
static void Main(string[] args)
{
ServiceHost host = new ServiceHost(typeof(HelloService));
BasicHttpBinding bind = new BasicHttpBinding();
host.AddServiceEndpoint(typeof(IhelloService), bind, "http://8080/myhelloservice");
host.Open();
Console.WriteLine("hello service is running");
Console.ReadKey();
}
}
Run Code Online (Sandbox Code Playgroud)
}
这段代码运行良好但是当我复制时,浏览器中的这个地址没有得到服务
我在 Ubuntu 18.04.3 LTS 下将 dotnetcore 应用程序作为服务运行时遇到困难。\n该应用程序在 Windows 上运行正常,大多数时候在 Ubuntu 上也运行正常。但是 Ubuntu 上的多个主机可能会启动服务,但 Web 主机在几个小时内不会初始化,然后在没有外部操作的情况下突然初始化,请参见下面的示例。Journalctl 在突然启动之前不会显示任何有趣的内容。
\n\nApr 23 18:52:01 DSK06511.avp.ru MyNode.Api[12276]: Running MyNode as console app\nApr 24 13:42:04 DSK06511.avp.ru MyNode.Api[12276]: IWebHost is built\nRun Code Online (Sandbox Code Playgroud)\n\n服务启动正常,状态为“正在运行”
\n\nroot@DSK06511:/home/monouser# service MyNode status\n\xe2\x97\x8f MyNode.service - MyNode\n Loaded: loaded (/etc/systemd/system/MyNode.service; enabled; vendor preset: enabled)\n Active: active (running) since Fri 2020-04-24 18:36:31 MSK; 5min ago\n Main PID: 10131 (MyNode)\n Tasks: 14 (limit: 4915)\n CGroup: /system.slice/MyNode.service\n \xe2\x94\x9c\xe2\x94\x8010131 /home/monouser/.octopus/Applications/OctopusServer/Production/MyNode.Linux/4.0.1.907/MyNode --console\n \xe2\x94\x9c\xe2\x94\x8010220 /home/monouser/.octopus/Applications/OctopusServer/Production/MyNode.Linux/4.0.1.907/MyNode --console\n \xe2\x94\x9c\xe2\x94\x8010264 /home/monouser/.octopus/Applications/OctopusServer/Production/MyNode.Linux/4.0.1.907/MyNode --console\n …Run Code Online (Sandbox Code Playgroud) 我不知道如何在没有通配符域的自托管 Gitlab 实例上设置 Gitlab 页面。例如,我有1台服务器,有3个公共IP地址和域名:
/etc/gitlab/gitlab.rb然后我像这样设置 Omnibus 配置:
external_url 'https://git.example.com'
nginx['enable'] = true
nginx['listen_addresses'] = ['10.8.0.10']
registry_external_url 'https://registry.example.com'
registry_nginx['enable'] = true
registry_nginx['listen_addresses'] = ['10.8.0.11']
registry_nginx['ssl_certificate'] = "/etc/gitlab/ssl/git.example.com.crt"
registry_nginx['ssl_certificate_key'] = "/etc/gitlab/ssl/git.example.com.key"
pages_external_url 'https://pages.example.com'
pages_nginx['enable'] = false
gitlab_pages['enable'] = true
gitlab_pages['cert'] = "/etc/gitlab/ssl/pages.example.com.crt"
gitlab_pages['cert_key'] = "/etc/gitlab/ssl/pages.example.com.key"
gitlab_pages['external_http'] = ['10.8.0.12:80']
gitlab_pages['external_https'] = ['10.8.0.12:443']
Run Code Online (Sandbox Code Playgroud)
例如,我有一个位于https://git.example.com/somegroupname/project的项目。我可以通过https://registry.example.com/somegroupname/project访问该项目的容器注册表,并使用命令拉取 Docker 映像docker pull registry.example.com/somegroupname/project。
我知道 GitLab Pages …
当"成本"这个话题出现时,我和同事正在讨论WFC服务.
问题是:
鉴于IIS托管的WCF服务和Windows服务托管的WCF服务完全相同,如果它们都接受相同的负载,哪个服务在内存和CPU周期方面会更"昂贵"?
我们不关心初始启动编码,安装或配置(IIS似乎旨在提供更简单的体验),只是运行服务的简单成本.
我写了一个自托管的SignalR应用程序(一个Windows服务).现在我想将NancyFx添加到这个应用程序来处理UI - 一个没有任何外部依赖关系的自给自足的插件式Web应用程序.
我知道我们可以拥有一个自托管的SignalR应用程序.我们也可以拥有一个自托管的NancyFx应用程序.
现在,如何将这两者结合在一个应用程序中?哪一个应该主持哪一个?
我正在为一个组件编写bdd测试,该组件将启动phantomjs并在我的站点上点击特定路由并对其进行处理.因为该组件基本上是关于幻像实例的自动化,所以无法轻易地删除http请求.
所以我想要一个自托管的端点,它将存根我想要的数据.因为这是一个单元测试,我认为它对于它独立运行非常重要,所以我做了类似这样的事情:
async Task can_render_html_for_slide_async() {
var config = new HttpSelfHostConfiguration("http://localhost:54331");
config.Routes.MapHttpRoute("Controller", "{controller}", new {});
using (var server = new HttpSelfHostServer(config)) {
server.OpenAsync().Wait();
var client = new HttpClient();
var resp = await client.GetStringAsync(config.BaseAddress+"/Stub");
Console.WriteLine(resp);
}
}
public class StubController : ApiController
{
public string Get() {
return "Booyah";
}
}
Run Code Online (Sandbox Code Playgroud)
哪个得到了我
AddressAccessDeniedException : HTTP could not register URL http://+:54331/
Run Code Online (Sandbox Code Playgroud)
我知道netsh或Admin模式是必需的,但我不明白为什么.例如,Nodejs在Windows上运行得很好但没有这样的要求.
self-hosting ×10
c# ×4
.net ×3
wcf ×3
nancy ×2
owin ×2
signalr ×2
.net-core ×1
acl ×1
asp.net-mvc ×1
firewall ×1
gitlab ×1
gitlab-api ×1
gitlab-pages ×1
iis ×1
linux ×1
service ×1
windows ×1