Disable or Mock a background service in xunit .net core - asp.net-core

I have this line of code in my startup.cs in my project
services.AddHostedService<AkkaService>(sp => (AkkaService)sp.GetRequiredService<IEasyProcessor>());
I need to disable this line when I run my tests .I don't want use environment variable .I just need to mock or disable this service .
I Mock this service in my test solution like this :
Mock<IEasyProcessor> easyprocessor= new Mock<IEasyProcessor>();
services.AddScoped<IEasyProcessor>(processor =>{ return easyprocessor.Object; });
But I get this error :
System.InvalidCastException: 'Unable to cast object of type 'Castle.Proxies.IEasyProcessorProxy' to type 'easy.api.Akka.AkkaService'.'

Related

Assembly name was changed after deployment in core web api application

I am using VS 2019 to develop core.net web Api. I am trying to read the all methods and Parameters inside my controller. I am using Repository pattern to develop API.
Below is the code from my repository.
var method = MethodBase.GetCurrentMethod();
_log4net.Info("Assembly Name : " + Assembly.GetCallingAssembly().FullName);
_log4net.Info("Method Name : " + method.Name);
_log4net.Info("Repository Name :" + method.ReflectedType.FullName);
var result =
((System.Reflection.TypeInfo)Assembly.GetCallingAssembly().GetTypes().Where(type
=> type.FullName.Contains("AsmeController")).FirstOrDefault()).DeclaredMethods;
_log4net.Info(result);
Log's:
In Debug Mode:
After deployment in IIS
This code is working as expected and returns the list of Method Info in Debug mode and not working and return Null in Release mode even after deployed in IIS.
As i observed using logs, Assembly name was changing Demo.dll to “ Assembly Name : Anonymously Hosted DynamicMethods Assembly “ after deployment.
Please give me suggestions to solve this problem.
For the work around i am directly reading the application dll, Instead of reading current assembly. So that i can able to access the all info from there.
string assemblyFile = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location + "\\Demo.dll");
Assembly testAssembly = Assembly.LoadFile(assemblyFile);
var result = ((TypeInfo)testAssembly.GetTypes().Where(type => type.FullName.Contains("AsmeController")).FirstOrDefault()).DeclaredMethods;

unexpected GET request on karma unit testing

I am using karma to test a controller in my angular app, the app it self works as expected but unit test throws error: unexpected request: GET views/home.html
as I got the problem is by ui-router and $httpBackend. so one approach was caching by ‘karma-preprocessor’ but what I have done is:
var MenuController, scope, $httpBackend;
// Initialize the controller and a mock scope
beforeEach(inject(function($controller, _$httpBackend_, $rootScope, menuService) {
// place here mocked dependencies
$httpBackend = _$httpBackend_;
$httpBackend.whenGET("views/header.html").respond(200,'');
$httpBackend.expectGET("http://localhost:3000/dishes").respond([{ ....
but the GET error shows for every template in my views folder.
NOW is there any way to ignore all templates and any other unexpected GET requests?
You can create a regular expression that matches all the templates requests:
httpBackend.whenGET(/views\/.*\.html/).respond(200, {}); or only
httpBackend.whenGET(/views\//).respond(200, {})

Using Kentico API from LINQPad is throwing an exception

I am trying to call Kentico API from LINQPad, but getting the following exception:
[AbstractProvider.GetProvider]: The object type 'cms.document' is missing the provider type configuration
My code is:
void Main()
{
var pages = DocumentHelper.GetDocuments("CMS.MenuItem").Path("/", PathTypeEnum.Children);
pages.Dump();
}
Note: I tested the code from Visual Studio, it works, but not from LINQPad.
The problem is that during the initial discovery Kentico looks only at the following paths:
AppDomain.CurrentDomain.BaseDirectory
AppDomain.CurrentDomain.RelativeSearchPath
Which in case of LINQPad are C:\Program Files (x86)\LINQPad4\ and null. Therefore the providers do not get resolved.
I've tried running the code in a new AppDomain but it doesn't seem to work in LINQPad. I suggest submitting this to Kentico as an idea or an issue.
A workaround to this would be copying the LINQPad executable to a location of Kentico DLLs - e.g. C:\inetpub\wwwroot\Kentico82\Lib. That works just fine.
Update (thx to Joe Albahari):
If you wrap your code in this:
var appDomain = Util.CreateAppDomain ("AD", null, new AppDomainSetup
{
PrivateBinPath = #"C:\inetpub\wwwroot\Kentico82\CMS\bin",
});
appDomain.DoCallBack(() => { /* your code */ });
you'll be able to execute it. However, you can't Dump() it to the output window. But you can write it to a text file for example. If you experience the following error:
FileNotFoundException: Could not load file or assembly 'LINQPad, Version=1.0.0.0, Culture=neutral, PublicKeyToken=21353812cd2a2db5' or one of its dependencies. The system cannot find the file specified.
Go to Edit -> Preferences -> Advanced -> Run each query in its own process and turn it off.

Can't find method error using WL.Server.setActiveUser()

I'm trying to determine the best approach for executing business logic in a Push adapter. I've run the example PushAdapter (Module_07_04_nativeAPIForiOSPush) successfully from my local environment, but, adding WL.Server.setActiveUser() throws an error.
I'm running the demo PushAdapter adapter locally in Worklight Studio (6.0.0.201309171829), added as the first line in the adapter:
WL.Server.setActiveUser("PushAppRealm",userId);
...
Deployed the adapter change, run with same params and get this error in the Worklight console:
Can't find method com.worklight.integration.js.JavaScriptIntegrationLibraryImplementation.setUserIdentity(string,string). (/integration.js#36)
FWLSE0101E: Caused by: [project Module_07_04_nativeAPIForiOSPush]null
The adapter runs without any problems without this line. I'm trying to set the active user because I want to get the user's preferences next to determine business logic on whether to create the notification. Is there another approach?
I've also run this in a new workspace (after I applied the Fix Pack 1 to WL Studio 6), but, same result.
Questions are 1) why getting this error?, and 2) is this a valid approach?
Thanks.
var userIdentity = {
userId: "userid",
displayName: "userid",
attributes: {
foo: "bar"
}
};
WL.Server.setActiveUser("PushAppRealm", userIdentity);
This should work. However for this sample you should not be explicitly setting the user identity.The method WL.Server.setActiveUser() is used to set the user identity in case of adapter based authentication.This sample uses form based authentication.

wsdl2java code generation for lists of custom objects

I would like to know if the tool "wsdl2java" (Axis2) is able to generate stubs that support getting list of custom ojects.
For instance, if I have a WS that have the following method:
public List<Device> getDevices(){
//...
}
Where Device is a custom class...
This tool can do that?
I changed the return data type of my Web Service to an array because of that:
http://www.ibm.com/developerworks/webservices/library/ws-tip-coding/index.html
And I had to do some changes (some namespaces) to the generated stub (I used ADB)...
I changed that because it was giving me an ADBException: Unexpected subelement ...