关于WCF 在ASP.NET MVC 中的问题

qq_36593316 2017-06-20 04:43:03
看了一篇文章http://www.cnblogs.com/scy251147/archive/2012/11/07/2759452.html
里面的有一步操作是这样的

其实很简单,我们只需要在服务入口处添加Service的EndPoint即可。
Uri uri = new Uri(ConfigurationManager.AppSettings["addr"]);
using(ServiceHost host = new ServiceHost(typeof(NikeSoftChat.ChatService),uri))
{
ServiceMetadataBehavior smb = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
if (smb == null)
{
host.Description.Behaviors.Add(new ServiceMetadataBehavior());
}

host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding(), "mex");
host.Open();

Console.WriteLine("Chat service listen on endpoint {0}", uri.ToString());
Console.WriteLine("Press ENTER to stop chat service...");
Console.ReadLine();
host.Abort();
host.Close();
}
复制代码
注意,黄色标记很重要,它能够暴露出元数据出来,如果没有黄色标记部分,利用SvcUtil生成net.tcp的代理类和配置文件将是不可能的。

然后我想问的问题就是:我在ASP.net mvc 里面该怎么把这段代码放进去呢。我尝试在Global.asax文件里面输入也不行,会报错。求解,改怎么搞?简单来说就是我怎么在ASP.NET mvc 项目里面暴露元数据,从而使用SvcUtil.exe工具生成配置文件。
...全文
338 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_36593316 2017-07-11
  • 打赏
  • 举报
回复
也是很莫名其妙
qq_36593316 2017-07-11
  • 打赏
  • 举报
回复
我重新安装ServiceModel 就解决了,运行这一段就可以了:C:/Windows/Microsoft.NET/Framework64/v4.0.30319/ServiceModelReg.exe -r
Jolly丶 2017-07-10
  • 打赏
  • 举报
回复
引用 2 楼 sp1234 的回复:
在 asp.net 中就应该按照 asp.net 教程所讲的方式来创建 WCF 服务。 asp.net 跟 console 机制是不一样的。asp.net 只是被 web服务进程调用的组件,它自己不能控制核心的注册任务,它直接承载 web 服务器系统转过来的 WCF 请求,它先天已经有一套服务机制,它自己不用什么 ServiceHost。
大牛推荐几本书看看、。了解下.net更深层次的东西。我关注你了。看了你的回复我一愣一愣的。。什么机制。什么组件完全不懂。
  • 打赏
  • 举报
回复
在 asp.net 中就应该按照 asp.net 教程所讲的方式来创建 WCF 服务。 asp.net 跟 console 机制是不一样的。asp.net 只是被 web服务进程调用的组件,它自己不能控制核心的注册任务,它直接承载 web 服务器系统转过来的 WCF 请求,它先天已经有一套服务机制,它自己不用什么 ServiceHost。
qq_36593316 2017-06-20
  • 打赏
  • 举报
回复
有人吗,帮忙顶一下,实在是没分了,还有4分.
This one hundred page book focuses exclusively on how you can best use the ASP.NET MVC 4 Framework to build world-class REST services using the Web API. It sets aside much of what the ASP.NET MVC Framework can do, and focuses exclusively on how the Web API can help you build web services. You will not find any help on CSS, HTML, JavaScript, or jQuery. Nor will you find any help on the Razor view engine, HTML Helpers, or model binding. If you need this information then Pro ASP.NET MVC 4 is your perfect book. ASP.NET MVC 4 and the Web API: Building a REST Service from Start to Finish helps you build cutting-edge REST services using ASP.NET MVC 4 and the Web API in more depth and detail than any other resource. ASP.NET MVC has always been a good platform on which to implement REST, but with the advent of the Web API it has now become even better. This book will show you why it's great and how to get the most from it. Author Jamie Kurtz will take you from zero to full-blown REST service hero in no time at all. And you'll even learn how to incorporate some popular open source tools along the way: little or no experience with ASP.NET or the MVC Framework is required. What you’ll learn How to design a REST API Understanding MVC and Web API controller activation Utilizing open source tools for common tasks Automatic lifetime management for database connections and transactions Easily secure a REST service, using standards-based authentication and authorization How to choose between using WCF or using the Web API Who this book is for This book is written for architects and developers wanting to understand how to quickly and easily build REST services on the ASP.NET platform. No prior knowledge of ASP.NET or MVC Framework is required; however, it assumed you have some experience with the .NET Framework, Visual Studio, and C#. Table of Contents 1. ASP.NET MVC as a Service Framework 2. What is RESTful? 3. Designing Our Sample REST API 4. Building the Environment and Creating the Source Tree 5. Controllers, Dependencies, and Managing the Database Unit of Work 6. Securing the Service 7. Putting It All Together

1,978

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 其他语言讨论
社区管理员
  • 其他语言社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧