How to Import IModuleManager Inside a Module - prism

The Question About PrismApplication. I Use Prism4 with MEF on a WPF Application,
The Error Occurred When Import IModuleManager Inside a Module
error:The exception message was: GetExportedValue cannot be called before prerequisite import 'Microsoft.Practices.Prism.MefExtensions.Modularity.MefModuleManager..ctor(Parameter="moduleInitializer", ContractName="Microsoft.Practices.Prism.Modularity.IModuleInitializer")' has been set.
[ModuleExport(typeof(IPlugIn))]
public class NavModule : IPlugIn
{
[Import]
private IRegionManager RegionManger;
[Import]
private IModuleManager ModuleManager;
public string Caption
{
.....
}
public void Initialize()
{
.....
}
}
Thanks in advance!

You may find a proper answer to your same question on the following CodePlex thread:
Problem with importing IModuleManager in a Module
It seems that there is a timing issue when composing the IModuleInitializer, which causes to throw the exception you described. However, this only happens with Modules registered in Code through the AggregateCatalog.
You can see the issue that has already been reported in the following link, with a workaround posted by the community:
Importing IModuleManager Inside a Module with Mef
I hope this helped you,
Regards.

Related

How to import classes as interfaces using Import / ImportMany in Prism?

I'm trying to import classes of a certain interface type. I already looked for help (e.g. here), but I can't seem to get it working.
First, I tried the following:
public interface ISomeInterface
{
...
}
[Export(typeof(ISomeInterface))]
public class SomeImplementation: ISomeInterface
{
...
}
[ImportingConstructor]
public ScannerMainViewModel([ImportMany(typeof(ISomeInterface))] IEnumerable<ISomeInterface> someImplementations)
{
...
}
But someImplementations is null.
Then I removed the ImportMany from the constructor and used it on the field
[ImportMany(typeof(ISomeInterface))]
private IEnumerable<ISomeInterface> someImplementations;
with the same result.
I then removed the ImportMany completely and only imported my one class this way:
[ImportingConstructor]
public ScannerMainViewModel(ISomeInterface someImplementation)
{
...
}
This lead to the folling error:
An exception has occurred while trying to add a view to region 'MainRegion'.
- The most likely causing exception was was: 'System.InvalidOperationException: No public constructor is available for type Common.Interfaces.IAccountScanner. ---> Unity.Exceptions.InvalidRegistrationException: Eine Ausnahme vom Typ "Unity.Exceptions.InvalidRegistrationException" wurde ausgelöst.
What am I doing wrong? I also tried to use names for contracts, but that didn't work too.

Something missing using micronaut filter

I am attempting to implement a filter in a micronaut microservice, using the example code documented in Section 6.18 of the documentation:
https://docs.micronaut.io/latest/guide/index.html#filters
I have a HelloWord service that is essentially the same as the service provided on the documentation, with a controller that goes to "/hello" (as documented). I am also using the same TraceService and trace filter that is provided in Section 6.18. I am compiling and running the server without problems.
Unfortunately, the filter is not being engaged when I test the microservice.
I am pretty sure that something is missing in my code, but as I said I am using the same code that is in the example:
TraceService Class
import io.micronaut.http.HttpRequest;
import io.reactivex.Flowable;
import io.reactivex.schedulers.Schedulers;
import org.slf4j.*;
import javax.inject.Singleton;
#Singleton
public class TraceService {
private static final Logger LOG = LoggerFactory.getLogger(TraceService.class);
Flowable<Boolean> trace(HttpRequest<?> request) {
System.out.println("TRACE ENGAGED!");
return Flowable.fromCallable(() -> {
if (LOG.isDebugEnabled()) {
LOG.debug("Tracing request: " + request.getUri());
}
// trace logic here, potentially performing I/O
return true;
}).subscribeOn(Schedulers.io());
}
}
Trace Filter
import io.micronaut.http.*;
import io.micronaut.http.annotation.Filter;
import io.micronaut.http.filter.*;
import org.reactivestreams.Publisher;
#Filter("/hello/**")
public class TraceFilter implements HttpServerFilter {
private final TraceService traceService;
public TraceFilter(TraceService traceService) {
System.out.println("Filter created!");
this.traceService = traceService;
}
#Override
public Publisher<MutableHttpResponse<?>> doFilter(HttpRequest<?> request, ServerFilterChain chain) {
System.out.println("Filter engaged!");
return traceService.trace(request)
.switchMap(aBoolean -> chain.proceed(request))
.doOnNext(res -> res.getHeaders().add("X-Trace-Enabled", "true")
);
}
}
The Controller
import io.micronaut.http.annotation.*;
#Controller("/hello")
public class HelloController {
#Get("/")
public String index() {
return "Hello World";
}
}
Note that the controller uses code from Section 2.2 of the documentation:
https://docs.micronaut.io/latest/guide/index.html#creatingServer
I did a number of things to try and see what was happening with the filter, including putting little printouts in strategic parts of the Service and the filter. These printouts are not printing out, which tells me that the filter is not being created or used by Micronaut.
Clearly I am missing somethning. I suspect that there is something I need to do in order to get the system to engage the filter. Unfortunately the documentation just tells how to make the filter, not how to use it in the microservice. Furthermore, there don't appear to be any complete code examples that tell how to make the request system utilize the filter (maybe there is an annotation I need to add to the controller???).
Could someone tell me what I am missing? How do I get the filter to work? At the very least, could someone provide a complete example of how to create the filter and use it in an actual microservice?
Problem solved.
It actually helps a great deal if one puts the filter and service files in the right place. It was late when I made the files and I put them in the test area, not the development area. Once placed in the right place, the filter was properly injected into the microservice.
Sorry for the waste of space here, folks. Is there any way a poster can delete an embarrassing post?

How to implement progressable image upload in Xamarin

I have Xamarin Forms app in which image is uploaded to server. I would like to show progress bar on UI. I am trying to create it with this reference. I created class ProgressableStreamContent:
public class ProgressableStreamContent : HttpContent
{
public ProgressableStreamContent(HttpContent content, Action<int> progress)
{...}
protected override Task SerializeToStreamAsync(Stream stream, TransportContext context)
{...}
}
Problem is in parameter of SerializeToStreamAsync procedure where TransportContext is not recognized as class. Error message is: "The type or namespace name 'TransportContext' cannot be found (are you missing a using directive or an assembly reference)?". When I navigate to HttpContent, there is:
public abstract class HttpContent : IDisposable
{
...
protected abstract Task SerializeToStreamAsync(Stream stream, Net.TransportContext context);
}
So it is not writen TransportContext, but Net.TransportContext. Does it mean that it is internal? How can I use this class in my code? I tried to put System.Net.TransportContext without success.
I haven't installed Microsoft.Net.Http Nuget Package. Now it is compiling correctly. How can I delete question?

Robospice Impossible to start SpiceManager as no service of class

I am trying to use following libraries to develop my app.
Robospice
Gson
and Spring for android
To do so, In my gradle file I have following dependencies added
compile 'com.octo.android.robospice:robospice-spring-android:1.4.13'
compile 'com.google.code.gson:gson:2.2.4'
And in manifest file, I have following lines inserted inside application tag.
<service
android:name="com.octo.android.robospice.GsonSpringAndroidSpiceService"
android:exported="false" />
Now, I created a base class that is used as Base Activity. And, I have did this in the following way:
public class BaseSpiceActivity extends Activity {
private SpiceManager spiceManager = new SpiceManager(GsonSpringAndroidSpiceService.class);
#Override
protected void onStart() {
spiceManager.start(this);
super.onStart();
}
#Override
protected void onStop() {
spiceManager.shouldStop();
super.onStop();
}
protected SpiceManager getSpiceManager() {
return spiceManager;
}
}
And then I used that base class to extend my own class where I had to use spice service request.
SimpleTextRequest text = new SimpleTextRequest(placeUrl);
getSpiceManager().execute(text, "place", DurationInMillis.ONE_MINUTE, new RequestListener<String>() {
//Other functions and codes
}
)
But when the above code gets executed, I am getting following error
E/AndroidRuntime﹕ FATAL EXCEPTION: SpiceManagerThread 0
java.lang.RuntimeException: Impossible to start SpiceManager as no service of class : com.octo.android.robospice.SpiceService is registered in AndroidManifest.xml file !
at com.octo.android.robospice.SpiceManager.checkServiceIsProperlyDeclaredInAndroidManifest(SpiceManager.java:1287)
at com.octo.android.robospice.SpiceManager.tryToStartService(SpiceManager.java:1168)
at com.octo.android.robospice.SpiceManager.run(SpiceManager.java:247)
at java.lang.Thread.run(Thread.java:856)
I have been trying to solve the issue from many hours. But unfortunately couldn't. Please help me out.
I am 100% sure you got messed somewhere. Search for new SpiceManager(SpiceService.class); in your code, and you will find out that you do use this service instead of the desired GsonSpringAndroidSpiceService.
You need to declare your service in your manifest.xml
Like this
<service android:name=".SampleRetrofitService"
android:exported="false"/>
Try to find out where using SpiceManager.
In my case, I named a activity SpiceActivity, and others extend it.
But robospice library also offer a activity named SpiceActivity. And it uses new SpiceManager(SpiceService.class)
So activity extends the wrong SpiceActivity which use SpiceService, then cause the error.

Why sonar(JaCoCo) is asking me to test my packages?

I am trying to close some test holes in my application and found that JaCoCo sonar plugin is giving me a smaller coverage in my enums because it thinks I should test the Package names.
Why is that?
It's showing me a 97% coverage in one of my enums and displaying a red line on top of the package declaration like this, telling me to test it... it does that in all Enums and on Enums only.
I came here looking for the answer to this, and after some more digging I discovered that it's due to some static methods that can be found in the bytecode of the compiled enum class which Jacoco is expecting to be covered. After some experimentation, I came up with the following superclass to use for unit tests which are focused on enums, with JUnit 4. This resolved my coverage problems with enums.
import org.junit.Test;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import static org.junit.Assert.assertEquals;
public abstract class EnumTest {
#Test
public void verifyEnumStatics() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Class e = getEnumUnderTest();
Method valuesMethod = e.getMethod("values");
Object[] values = (Object[]) valuesMethod.invoke(null);
Method valueOfMethod = e.getMethod("valueOf", String.class);
assertEquals(values[0], valueOfMethod.invoke(null, ((Enum)values[0]).name()));
}
protected abstract Class getEnumUnderTest();
}
And then use it like this:
public class TravelTypeTest extends EnumTest {
#Override
protected Class getEnumUnderTest() {
return TravelType.class;
}
// other test methods if needed
}
This is a rough first attempt - it doesn't work on enums that for whatever reason don't have any entries, and doubtless there are better ways to get the same effect, but this will exercise the generated static methods by ensuring that you can retrieve the values of the enum, and that if you pass the name of the first enum entry to the valueOf() method you will get the first enum entry back.
Ideally we'd write a test that searches for all enums in the packages under test and exercise them in the same way automatically (and avoid having to remember to create a new test class for each new enum that inherits from EnumTest), but I don't have many enums so I haven't felt any pressure to attempt this yet.

Resources