c# - CSLAs WCF usage when running XUnit tests in vs test explorer causes an Impersonation Exception -
we're using xunit 2.1.0 nuget relating runners, console , visualstudio documented here under "running tests visual studio" heading , relating content.
i'm using visual studio 2015 enterprise update 2. thing that's reasonably out of date csla, we're on 4.0.1 (i think 5 years old?)
when run tests require dataportal fetch, test falls on dataportal fetch attempts sent server. wcf throws "system.servicemodel.faultexception" stating "invalid token impersonation - cannot duplicated." it's important note none of tests attempt impersonate user. falling on happens in test attempts use csla dataportal call. we've moved on xunit 1.x 2.x through nuget, used run xunit xunit runner when testing our tests locally deprecated. tests ran absolutely fine through both gui , console runner xunit 1.x. have use visual studio runner xunit 2.x getting crazy exception.
edit: if run xunit 2.x console runner outside of visual studio tests fine on 2.x, it's visual studio side of things not working.
stack trace below:
server stack trace: @ system.servicemodel.channels.servicechannel.throwiffaultunderstood(message reply, messagefault fault, string action, messageversion version, faultconverter faultconverter) @ system.servicemodel.channels.servicechannel.handlereply(proxyoperationruntime operation, proxyrpc& rpc) @ system.servicemodel.channels.servicechannel.call(string action, boolean oneway, proxyoperationruntime operation, object[] ins, object[] outs, timespan timeout) @ system.servicemodel.channels.servicechannelproxy.invokeservice(imethodcallmessage methodcall, proxyoperationruntime operation) @ system.servicemodel.channels.servicechannelproxy.invoke(imessage message) exception rethrown @ [0]: @ system.runtime.remoting.proxies.realproxy.handlereturnmessage(imessage reqmsg, imessage retmsg) @ system.runtime.remoting.proxies.realproxy.privateinvoke(messagedata& msgdata, int32 type) @ csla.server.hosts.iwcfportal.fetch(fetchrequest request) @ csla.dataportalclient.wcfproxy.fetch(type objecttype, object criteria, dataportalcontext context) in d:\dev\insight\trunk\source\lib\csla .net\4.0\source\csla\dataportalclient\wcfproxy.cs:line 162 @ csla.dataportal.fetch(type objecttype, object criteria) in d:\dev\insight\trunk\source\lib\csla .net\4.0\source\csla\dataportal.cs:line 245 @ csla.dataportal.fetch[t](object criteria) in d:\dev\insight\trunk\source\lib\csla .net\4.0\source\csla\dataportal.cs:line 170
again works fine if run tests test runner, either old xunit test runner or cruisecontrol.net example (we use cc.net continuous integration , runs tests fine)
i think more of issue way visual studio test runner sets current user principal. of other test runners appear use empty genericprincipal, whereas vs 1 appears set current principal impersonated version of current windows identity. means error seeing when csla.net attempts impersonate again.
the issue discussed in detail in blog post relating nunit: http://www.ienumerable.it/2015/03/21/setting-up-good-fixture.html
a relatively simple way solve xunit (adapted blog above) set beforeaftertestattribute set genericprincipal before test runs , restore original principal afterwards. guarantees run same principal, regardless of test runner used.
public class requiresgenericprincipalattribute : beforeaftertestattribute { private iprincipal _originalprincipal; public override void before(methodinfo methodundertest) { _originalprincipal = system.threading.thread.currentprincipal; system.threading.thread.currentprincipal = new genericprincipal(new genericidentity(""), new string[] { }); base.before(methodundertest); } public override void after(methodinfo methodundertest) { base.after(methodundertest); system.threading.thread.currentprincipal = _originalprincipal; } }
Comments
Post a Comment