小编Tra*_*lig的帖子

为什么找不到我的自定义WCF行为扩展元素类型?

我有一个包含两个项目的解决方案.一个项目是ASP.NET Web应用程序项目,一个是类库.Web应用程序具有对类库的项目引用.这些都没有强烈的名称.

在类库中,我称之为"Framework",我有一个端点行为(一个IEndpointBehavior实现)和一个配置元素(一个派生自BehaviorExtensionsElement的类).配置元素是我可以通过配置将端点行为附加到服务.

在Web应用程序中,我有一个支持AJAX的WCF服务.在web.config中,我将AJAX服务配置为使用我的自定义行为.配置的system.serviceModel部分非常标准,如下所示:

<system.serviceModel>
 <behaviors>
  <endpointBehaviors>
   <behavior name="MyEndpointBehavior">
    <enableWebScript />
    <customEndpointBehavior />
   </behavior>
  </endpointBehaviors>
 </behaviors>
 <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
 <services>
 <service name="WebSite.AjaxService">
  <endpoint
           address=""
           behaviorConfiguration="MyEndpointBehavior"
           binding="webHttpBinding"
           contract="WebSite.AjaxService" />
  </service>
 </services>
 <extensions>
  <behaviorExtensions>
   <add
       name="customEndpointBehavior"
       type="Framework.MyBehaviorExtensionsElement, Framework, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
  </behaviorExtensions>
 </extensions>
</system.serviceModel>
Run Code Online (Sandbox Code Playgroud)

在运行时,这非常有效.启用AJAX的WCF服务正确使用我自定义配置的端点行为.

问题是当我尝试添加新的AJAX WCF服务时.如果我执行Add - > New Item ...并选择"启用了AJAX的WCF服务",我可以看到它添加.svc文件和代码隐藏,但是当它更新web.config文件时,我收到此错误:

配置文件不是WCF服务库的有效配置文件.

无法加载为扩展名'customEndpointBehavior'注册的类型'Framework.MyBehaviorExtensionsElement,Framework,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null'.

显然,配置完全有效,因为它在运行时完美运行.如果我暂时从我的行为配置中删除该元素,然后添加启用了AJAX的WCF服务,那么一切都顺利进行.

不幸的是,在一个更大的项目中,我们将拥有各种配置的多个服务,暂时删除所有自定义行为将容易出错.虽然我意识到我可以不使用向导并且手动完成所有操作,但不是每个人都可以,并且能够使用产品,因为它本意使用 - 向导和所有.

为什么找不到我的自定义WCF行为扩展元素类型?

更新/澄清:

  • 它确实在运行时工作,而不是设计时间.
  • 当我尝试添加服务时,Framework程序集位于Web项目的bin文件夹中.
  • 虽然我可以手动添加服务("没有配置"),但我需要开箱即用的项目模板才能工作 - 这就是问题的整个目标.
  • 在Visual Studio 2008中可以看到此问题.在VS 2010中,这似乎已得到解决.

我在Microsoft Connect上提交了此问题,但事实证明您必须将自定义配置元素放在GAC中或将其放在IDE文件夹中.他们不会修复它,至少目前是这样.我已经发布了他们提供的解决方法作为这个问题的"答案".

.net wcf exception-handling wcf-extensions

47
推荐指数
3
解决办法
4万
查看次数

如何为.NET可移植类库创建单元测试程序集?

我试图对我创建的可移植类库进行单元测试,并且我想确保它使用与其目标相同的框架子集进行测试.

根据Visual Studio ALM + Team Foundation Server博客,MSTest单元测试框架在Visual Studio 2012 RC中转换为PCL; 但是,我无法创建可移植的类库,然后在VS2012 RTM中引用MSTest框架.

  • 在"参考"对话框中浏览显示没有单元测试组件被注册为与PCL兼容.
  • 在项目文件中添加手动GAC引用,以Microsoft.VisualStudio.QualityTools.UnitTestFramework产生未找到引用的构建错误.
  • 添加手动直接程序集引用以C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ReferenceAssemblies\v4.0\Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll产生构建警告,说明UnitTestFramework程序集引用了不兼容的mscorlib版本.

我确实找到了(感谢早期的答案),有一个项目类型Unit Test Library (Windows Store apps)引用了不同的 MSTest程序集C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0\ExtensionSDKs\MSTestFramework\11.0\References\CommonConfiguration\neutral\Microsoft.VisualStudio.TestPlatform.UnitTestFramework.dll.此项目类型创建一个小的无UI Windows应用程序应用程序...完成清单和所有内容.它也不允许我指定我所针对的框架 - 它似乎仅适用于Windows应用商店应用.

潜在的错误假设下,我应该使用单元测试程序集测试我的可移植类库项目,该单元测试程序集的目标是与被测试库相同的框架子集...

如何为.NET可移植类库创建单元测试程序集?

(我对其他同样针对PCL的框架持开放态度,我目前还没有意识到MSTest之外的其他解决方案已经考虑到了这一点.)

.net mstest portable-class-library

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

多层C#应用程序中的异步/等待

我在高流量场景中有一个多层C#MVC4 Web应用程序,它使用依赖注入来处理各种存储库.这非常有用,因为它易于测试,在生产中我们可以轻松配置特定控制器的存储库.所有控制器都继承自AsyncController,因此返回Task<JsonResult>Task<ActionResult>真正帮助我们的服务器的操作方法可以扩展到更多用户并解决可怕的线程饥饿问题.一些存储库使用Web服务,我们绝对可以使用Async/Await来提供可伸缩性优势.在其他情况下,有些可能会使用无法安全线程的非托管服务,其中async/await将不会带来任何好处.每个存储库都是一个接口IRepository的实现.简而言之,每种实现方式在获取数据方面都有很大差异.在部署时使用web.config更改和Autofac模块选择此配置.

在这样的应用程序中实现async/await的一些推荐方法是什么?为了使模式适合我现有的应用程序,我必须更改接口以返回Task<MyBusinessObject>. 但这不是一个实现细节吗? 我可以提供两个方法存根,GetDataGetDataAsync,但对我来说,不会允许灵活性我现在有IoC框架类似Autofac在那里我可以一切轻松更换,而无需更改代码.

Async/Await的一位微软开发人员发布了一篇博文," 我应该为我的同步方法公开一个异步包装器吗? ",这种问题就出现了.如果您的异步方法不是真正的异步(提供本机I/O好处),那么它实际上只会增加开销.换句话说,它不会提供任何可扩展性优势.

我只是想社区的其他成员如何解决这个问题.

c# interface repository-pattern async-await

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

如何在NuGet中基于目标框架指定条件依赖项?

我正在构建一个引用Microsoft CommonServiceLocator程序集的NuGet包.

那里有两个版本的Microsoft CommonServiceLocator:

我的项目是一个可移植类库,但由于它有时与Enterprise Library一起使用,我需要对"可移植版本"进行"有条件"引用,这样就没有冲突了.

  • 如果目标框架是完整的.NET 4.0/4.5,请使用原始的CommonServiceLocator包,以便人们也可以使用Enterprise Library位(也引用CommonServiceLocator包).
  • 如果目标框架是可移植的(或其他任何东西),请使用Portable.CommonServiceLocator包.

我在NuGet文档中看到了新的"组"功能,显示了如何在.nuspec文件中指定依赖项,我认为这将完成我想要的,但我不确定如何测试它.

这是我认为我需要做的事情,我希望有人可以验证我的方法或指出我正确的方向:

<dependencies>
  <group>
    <!-- Always include regardless of target framework -->
    <dependency id="Autofac" />
  </group>
  <group targetFramework="net40">
    <!-- Also include the full CSL if it's full framework -->
    <dependency id="CommonServiceLocator" />
  </group>
  <group targetFramework="portable-win+sl50+wp8">
    <!-- Otherwise include the Portable CSL -->
    <dependency id="Portable.CommonServiceLocator" />
  </group>
</dependencies>
Run Code Online (Sandbox Code Playgroud)

特别...

  • 我的targetFramework语法合适吗?我找不到任何示例,所以我不知道+分隔机制是否正确或是否应该以逗号分隔.
  • 默认组是否有效?具有未指定目标框架的那个组 - 将始终包含在内还是我需要在每个组中复制/粘贴它?

nuget nuspec

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

继续使用Cookie在Web API中使用OWIN和app.UseWebApi进行身份验证

我想在我的应用程序的MVC和API部分继续使用相同的cookie.我知道这不是很安全但仍然存在.如果我在VS中创建一个新的MVC项目,一切都有效,Web API是从Global.asax使用设置的GlobalConfiguration.Configure(WebApiConfig.Register).但是当我尝试使用OWIN来配置Web API时,我遇到了一个问题,即我的API控制器中的User总是为null.

这是我的代码Startup.cs:

var config = new HttpConfiguration();
WebApiConfig.Register(config);
app.UseWebApi(config);
Run Code Online (Sandbox Code Playgroud)

控制器工作,路由也一样,使用相同的WebApiConfig.cs文件.但是,现在我的API控制器中的用户为空.什么是从我的实例缺少的HttpConfiguration存在于GlobalConfiguration.Configuration

我需要使用我自己的实例HttpConfiguration而不是使用GlobalConfiguration.Configuration因为我打算使用Autofac并且它不能用于此处GlobalConfiguration提到的

编辑:

我的Startup.Auth.cs:

// Configure the db context, user manager and signin manager to use a single instance per request
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);

// Enable the application to use a cookie to store information for the signed in user
// and to use a cookie to temporarily store information about a …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc owin asp.net-web-api2

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

基类中的自动装配属性在 ASP.NET Core 2 中不起作用

我有一个 .Net Core 2 应用程序和一个继承自抽象基类的 API 控制器类:

 [Route("api/[controller]")]
 public class MyController : BaseController
Run Code Online (Sandbox Code Playgroud)

在基本控制器上,我有:

public ICommandProcessor CommandProcessor { get; set; }
Run Code Online (Sandbox Code Playgroud)

在我的 StartUp.cs 中,我有:

builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
                .Where(t => t.IsSubclassOf(typeof(BaseController)))
                .PropertiesAutowired();
Run Code Online (Sandbox Code Playgroud)

但是,CommandProcessor 属性始终为空。我尝试将它移到派生类并尝试直接注册类型,如下所示:

builder.RegisterType<MyController>().PropertiesAutowired();
Run Code Online (Sandbox Code Playgroud)

还有基类:

builder.RegisterType<BaseController>().PropertiesAutowired();
Run Code Online (Sandbox Code Playgroud)

我已经尝试了此处和此处以及其他帖子中的建议,但似乎没有任何效果,这让我认为这是 .Net Core 2 中的一个问题。

当我将 ICommandProcessor 注入控制器构造函数时,它连接良好。

值得一提的是,我在其他不是控制器的类上也试过这个,在启动时按如下方式注册:

builder.RegisterType<DomainEvents>().PropertiesAutowired();
Run Code Online (Sandbox Code Playgroud)

并且该属性也仍然为空。

编辑:

我在 github 上的 autofac 项目页面上提出了这个问题,他们建议我展示完整的 ConfigureServices 类,如下所示:

public IServiceProvider ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddMvc().AddControllersAsServices();

    // Identity server

    var builder = new ContainerBuilder();

    builder.RegisterModule(new CoreImplementationModule());
    builder.RegisterModule(new PortalCoreImplementationModule());

    builder.Register((context, _) => …
Run Code Online (Sandbox Code Playgroud)

dependency-injection inversion-of-control autofac .net-core asp.net-core

6
推荐指数
0
解决办法
788
查看次数

如何使用Xamarin和Autofac将构造函数依赖项注入到ViewModel中?

我有一个ViewModel,我想向它注入另一个类。我将Visual Studio与最新版本的Xamarin一起使用。我正在使用Autofac来注册解析依赖项。但是,我是新手,即使遇到的问题很简单,我仍然无法找到解决方案。

这是我要在其中注入另一个类的类:

public IMessagingCenterWrapper MessagingCenterWrapper;

public LoginViewModel(IMessagingCenterWrapper messagingCenterWrapper){
            MessagingCenterWrapper = messagingCenterWrapper;
        }
Run Code Online (Sandbox Code Playgroud)

然后在应用程序的入口点,我有一个函数来初始化容器,该容器注册并解析依赖项

static IContainer container{ get; set; }

public App ()
        {
            InitializeComponent();

            InitializeIOCContainer();
        }

void InitializeIOCContainer()
        {
            var builder = new ContainerBuilder();
            builder.RegisterType<LoginViewModel>();
            builder.RegisterType<MessagingCenterWrapper>().As<IMessagingCenterWrapper>();
            container = builder.Build();

            var wrapper = container.Resolve<IMessagingCenterWrapper>();
            var viewModel = container.Resolve<LoginViewModel>();
        }
Run Code Online (Sandbox Code Playgroud)

但是在登录视图中的行上构建时出现错误:

 BindingContext = new LoginViewModel();
Run Code Online (Sandbox Code Playgroud)

我收到错误消息是因为我没有在调用中初始化参数。

但是,如果这样做,我就不会破坏IoC模式的整个原理。最终,新的类调用将与其他依赖项嵌套在一起,我想避免这种情况。

所以我的问题是:如何实际将class参数注入构造函数中?

c# dependency-injection inversion-of-control autofac xamarin.forms

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

为什么我的项目参考不会被Visual Studio中的Web部署包装拾取?

我有一个包含两个项目的Visual Studio 2010解决方案:

  1. ASP.NET MVC 3 Web应用程序.
  2. 一个类库.

MVC应用程序具有对类库的项目引用.

当我使用Visual Studio中的"构建部署包"选项来使用我的Web应用程序构建zip文件进行部署时,它不包含类库.因此,在部署之后,由于缺少程序集,我得到一个例外.

我已经验证项目引用设置为"Copy Local = true".在构建和调试站点时,工作正常,类库位于bin文件夹中.只有在构建部署包时才会丢失它.

如果我从项目引用切换到直接程序集引用,指向bin/Debug/ClassLibrary.dll文件,该程序包将正确构建并包含类库.我只看到它是项目参考时的问题.

如何将类库作为项目引用以正确包含在Web应用程序包中?

asp.net package visual-studio-2010 msdeploy

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

如何在依赖于可移植类库的库上成功运行secannotate.exe?

我正在研究Autofac项目,试图将所有常见逻辑转换为可移植类库,并为特定功能添加特定于平台的库.

我的开发机器是Windows 8企业版(64位),我安装了VS 2012 Ultimate以及所有装饰.我没有安装任何以前的.NET框架,任何其他工具或任何额外的PCL专用工具.它是一个干净的,新的虚拟机,只有基础的东西.在此配置中,所有构建和测试都运行良好.

当我尝试在依赖于其中一个可移植类库的.NET 4.5(完整配置文件)库上运行secannotate.exe时,出现错误,指示我需要mscorlib 2.0.5.0.

这是一个示例错误.PCL是Autofac.dll; .NET 4.5完整配置文件库是Autofac.Configuration.dll.

Error running annotator: Could not find referenced assembly 'Assembly(Name=mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)'. Ensure that the reference paths and assemblies are setup correctly.
Microsoft (R) .NET Framework Security Transparency Annotator 4.0.30319.17929
Copyright (C) Microsoft Corporation.  All rights reserved.

Loaded assembly 'Autofac.Configuration' from 'C:\dev\opensource\autofac\trunk\build_output\bin\net40\Autofac.Configuration.dll'.
Resolving assembly 'Assembly(Name=mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)'.
Loaded assembly 'mscorlib' from 'C:\Windows\Microsoft.Net\assembly\GAC_64\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'.
Loaded referenced assembly from 'C:\Windows\Microsoft.Net\assembly\GAC_64\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'.
Using …
Run Code Online (Sandbox Code Playgroud)

.net code-access-security portable-class-library

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

是否可以使用Castle.DynamicProxy创建异步交互?

我们基本上有一个如下所示的类,它使用Castle.DynamicProxy进行拦截.

using System;
using System.Collections.Concurrent;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Castle.DynamicProxy;

namespace SaaS.Core.IoC
{
    public abstract class AsyncInterceptor : IInterceptor
    {
        private readonly ILog _logger;

        private readonly ConcurrentDictionary<Type, Func<Task, IInvocation, Task>> wrapperCreators =
            new ConcurrentDictionary<Type, Func<Task, IInvocation, Task>>();

        protected AsyncInterceptor(ILog logger)
        {
            _logger = logger;
        }

        void IInterceptor.Intercept(IInvocation invocation)
        {
            if (!typeof(Task).IsAssignableFrom(invocation.Method.ReturnType))
            {
                InterceptSync(invocation);
                return;
            }

            try
            {
                CheckCurrentSyncronizationContext();
                var method = invocation.Method;

                if ((method != null) && typeof(Task).IsAssignableFrom(method.ReturnType))
                {
                    var taskWrapper = GetWrapperCreator(method.ReturnType);
                    Task.Factory.StartNew(
                        async () => …
Run Code Online (Sandbox Code Playgroud)

asynchronous castle-dynamicproxy

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